control.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. buflen = len + 2;
  298. buf = tor_malloc_zero(buflen);
  299. set_uint16(buf, htons(event));
  300. memcpy(buf+2, body, len);
  301. get_connection_array(&conns, &n_conns);
  302. for (i = 0; i < n_conns; ++i) {
  303. if (conns[i]->type == CONN_TYPE_CONTROL &&
  304. conns[i]->state == CONTROL_CONN_STATE_OPEN &&
  305. conns[i]->event_mask & (1<<event)) {
  306. send_control_message(conns[i], CONTROL_CMD_EVENT, buflen, buf);
  307. if (event == EVENT_ERR_MSG) {
  308. _connection_controller_force_write(conns[i]);
  309. }
  310. }
  311. }
  312. tor_free(buf);
  313. }
  314. /** Called when we receive a SETCONF message: parse the body and try
  315. * to update our configuration. Reply with a DONE or ERROR message. */
  316. static int
  317. handle_control_setconf(connection_t *conn, uint32_t len, char *body)
  318. {
  319. int r;
  320. struct config_line_t *lines=NULL;
  321. if (config_get_lines(body, &lines) < 0) {
  322. log_fn(LOG_WARN,"Controller gave us config lines we can't parse.");
  323. send_control_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
  324. return 0;
  325. }
  326. if ((r=config_trial_assign(lines, 1)) < 0) {
  327. log_fn(LOG_WARN,"Controller gave us config lines that didn't validate.");
  328. if (r==-1) {
  329. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY,
  330. "Unrecognized option");
  331. } else {
  332. send_control_error(conn, ERR_INVALID_CONFIG_VALUE,"Invalid option value");
  333. }
  334. config_free_lines(lines);
  335. return 0;
  336. }
  337. config_free_lines(lines);
  338. if (options_act() < 0) { /* acting on them failed. die. */
  339. log_fn(LOG_ERR,"Acting on config options left us in a broken state. Dying.");
  340. exit(1);
  341. }
  342. send_control_done(conn);
  343. return 0;
  344. }
  345. /** Called when we receive a GETCONF message. Parse the request, and
  346. * reply with a CONFVALUE or an ERROR message */
  347. static int
  348. handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body)
  349. {
  350. smartlist_t *questions = NULL;
  351. smartlist_t *answers = NULL;
  352. char *msg = NULL;
  353. size_t msg_len;
  354. or_options_t *options = get_options();
  355. questions = smartlist_create();
  356. smartlist_split_string(questions, body, "\n",
  357. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  358. answers = smartlist_create();
  359. SMARTLIST_FOREACH(questions, const char *, q,
  360. {
  361. int recognized = config_option_is_recognized(q);
  362. if (!recognized) {
  363. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, q);
  364. goto done;
  365. } else {
  366. struct config_line_t *answer = config_get_assigned_option(options,q);
  367. while (answer) {
  368. struct config_line_t *next;
  369. size_t alen = strlen(answer->key)+strlen(answer->value)+3;
  370. char *astr = tor_malloc(alen);
  371. tor_snprintf(astr, alen, "%s %s\n", answer->key, answer->value);
  372. smartlist_add(answers, astr);
  373. next = answer->next;
  374. tor_free(answer->key);
  375. tor_free(answer->value);
  376. tor_free(answer);
  377. answer = next;
  378. }
  379. }
  380. });
  381. msg = smartlist_join_strings(answers, "", 0, &msg_len);
  382. send_control_message(conn, CONTROL_CMD_CONFVALUE,
  383. (uint16_t)msg_len, msg_len?msg:NULL);
  384. done:
  385. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  386. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  387. smartlist_free(answers);
  388. smartlist_free(questions);
  389. tor_free(msg);
  390. return 0;
  391. }
  392. /** Called when we get a SETEVENTS message: update conn->event_mask,
  393. * and reply with DONE or ERROR. */
  394. static int
  395. handle_control_setevents(connection_t *conn, uint32_t len, const char *body)
  396. {
  397. uint16_t event_code;
  398. uint32_t event_mask = 0;
  399. if (len % 2) {
  400. send_control_error(conn, ERR_SYNTAX,
  401. "Odd number of bytes in setevents message");
  402. return 0;
  403. }
  404. for (; len; len -= 2, body += 2) {
  405. event_code = ntohs(get_uint16(body));
  406. if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
  407. send_control_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
  408. "Unrecognized event code");
  409. return 0;
  410. }
  411. event_mask |= (1 << event_code);
  412. }
  413. conn->event_mask = event_mask;
  414. update_global_event_mask();
  415. send_control_done(conn);
  416. return 0;
  417. }
  418. /** Decode the hashed, base64'd password stored in <b>hashed</b>. If
  419. * <b>buf</b> is provided, store the hashed password in the first
  420. * S2K_SPECIFIER_LEN+DIGEST_LEN bytes of <b>buf</b>. Return 0 on
  421. * success, -1 on failure.
  422. */
  423. int
  424. decode_hashed_password(char *buf, const char *hashed)
  425. {
  426. char decoded[64];
  427. if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
  428. != S2K_SPECIFIER_LEN+DIGEST_LEN) {
  429. return -1;
  430. }
  431. if (buf)
  432. memcpy(buf, decoded, sizeof(decoded));
  433. return 0;
  434. }
  435. /** Called when we get an AUTHENTICATE message. Check whether the
  436. * authentication is valid, and if so, update the connection's state to
  437. * OPEN. Reply with DONE or ERROR.
  438. */
  439. static int
  440. handle_control_authenticate(connection_t *conn, uint32_t len, const char *body)
  441. {
  442. or_options_t *options = get_options();
  443. if (options->CookieAuthentication) {
  444. if (len == AUTHENTICATION_COOKIE_LEN &&
  445. !memcmp(authentication_cookie, body, len)) {
  446. goto ok;
  447. }
  448. } else if (options->HashedControlPassword) {
  449. char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
  450. char received[DIGEST_LEN];
  451. if (decode_hashed_password(expected, options->HashedControlPassword)<0) {
  452. log_fn(LOG_WARN,"Couldn't decode HashedControlPassword: invalid base64");
  453. goto err;
  454. }
  455. secret_to_key(received,DIGEST_LEN,body,len,expected);
  456. if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
  457. goto ok;
  458. goto err;
  459. } else {
  460. if (len == 0) {
  461. /* if Tor doesn't demand any stronger authentication, then
  462. * the controller can get in with a blank auth line. */
  463. goto ok;
  464. }
  465. goto err;
  466. }
  467. err:
  468. send_control_error(conn, ERR_REJECTED_AUTHENTICATION,"Authentication failed");
  469. return 0;
  470. ok:
  471. log_fn(LOG_INFO, "Authenticated control connection (%d)", conn->s);
  472. send_control_done(conn);
  473. conn->state = CONTROL_CONN_STATE_OPEN;
  474. return 0;
  475. }
  476. static int
  477. handle_control_saveconf(connection_t *conn, uint32_t len,
  478. const char *body)
  479. {
  480. if (save_current_config()<0) {
  481. send_control_error(conn, ERR_INTERNAL,
  482. "Unable to write configuration to disk.");
  483. } else {
  484. send_control_done(conn);
  485. }
  486. return 0;
  487. }
  488. static int
  489. handle_control_signal(connection_t *conn, uint32_t len,
  490. const char *body)
  491. {
  492. if (len != 1) {
  493. send_control_error(conn, ERR_SYNTAX,
  494. "Body of SIGNAL command too long or too short.");
  495. } else if (control_signal_act((uint8_t)body[0]) < 0) {
  496. send_control_error(conn, ERR_SYNTAX, "Unrecognized signal number.");
  497. } else {
  498. send_control_done(conn);
  499. }
  500. return 0;
  501. }
  502. static int
  503. handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body)
  504. {
  505. smartlist_t *elts;
  506. smartlist_t *lines;
  507. smartlist_t *reply;
  508. char *r;
  509. size_t sz;
  510. lines = smartlist_create();
  511. elts = smartlist_create();
  512. reply = smartlist_create();
  513. smartlist_split_string(lines, body, "\n",
  514. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  515. SMARTLIST_FOREACH(lines, char *, line,
  516. {
  517. tor_strlower(line);
  518. smartlist_split_string(elts, line, " ", 0, 2);
  519. if (smartlist_len(elts) == 2) {
  520. const char *from = smartlist_get(elts,0);
  521. const char *to = smartlist_get(elts,1);
  522. if (!is_plausible_address(from)) {
  523. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",from);
  524. } else if (!is_plausible_address(to)) {
  525. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",to);
  526. } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
  527. const char *addr = addressmap_register_virtual_address(
  528. strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
  529. tor_strdup(to));
  530. if (!addr) {
  531. log_fn(LOG_WARN,
  532. "Unable to allocate address for '%s' in MapAddress msg",line);
  533. } else {
  534. size_t anslen = strlen(addr)+strlen(to)+2;
  535. char *ans = tor_malloc(anslen);
  536. tor_snprintf(ans, anslen, "%s %s", addr, to);
  537. smartlist_add(reply, ans);
  538. }
  539. } else {
  540. addressmap_register(from, tor_strdup(to), 1);
  541. smartlist_add(reply, tor_strdup(line));
  542. }
  543. } else {
  544. log_fn(LOG_WARN, "Skipping MapAddress line with wrong number of items.");
  545. }
  546. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  547. smartlist_clear(elts);
  548. });
  549. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  550. smartlist_free(lines);
  551. smartlist_free(elts);
  552. r = smartlist_join_strings(reply, "\n", 1, &sz);
  553. send_control_done2(conn,r,sz);
  554. SMARTLIST_FOREACH(reply, char *, cp, tor_free(cp));
  555. smartlist_free(reply);
  556. tor_free(r);
  557. return 0;
  558. }
  559. /** Lookup the 'getinfo' entry <b>question</b>, and return
  560. * the answer in <b>*answer</b> (or NULL if key not recognized).
  561. * Return 0 if success, or -1 if internal error. */
  562. static int
  563. handle_getinfo_helper(const char *question, char **answer)
  564. {
  565. *answer = NULL; /* unrecognized key by default */
  566. if (!strcmp(question, "version")) {
  567. *answer = tor_strdup(VERSION);
  568. } else if (!strcmpstart(question, "desc/id/")) {
  569. routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/"));
  570. if (ri && ri->signed_descriptor)
  571. *answer = tor_strdup(ri->signed_descriptor);
  572. } else if (!strcmpstart(question, "desc/name/")) {
  573. routerinfo_t *ri = router_get_by_nickname(question+strlen("desc/name/"));
  574. if (ri && ri->signed_descriptor)
  575. *answer = tor_strdup(ri->signed_descriptor);
  576. } else if (!strcmp(question, "network-status")) {
  577. routerlist_t *routerlist;
  578. router_get_routerlist(&routerlist);
  579. if (!routerlist || !routerlist->routers ||
  580. list_server_status(routerlist->routers, NULL, answer) < 0) {
  581. return -1;
  582. }
  583. } else if (!strcmpstart(question, "addr-mappings/")) {
  584. time_t min_e, max_e;
  585. smartlist_t *mappings;
  586. if (!strcmp(question, "addr-mappings/all")) {
  587. min_e = 0; max_e = TIME_MAX;
  588. } else if (!strcmp(question, "addr-mappings/cache")) {
  589. min_e = 2; max_e = TIME_MAX;
  590. } else if (!strcmp(question, "addr-mappings/config")) {
  591. min_e = 0; max_e = 0;
  592. } else if (!strcmp(question, "addr-mappings/control")) {
  593. min_e = 1; max_e = 1;
  594. } else {
  595. return 0;
  596. }
  597. mappings = smartlist_create();
  598. addressmap_get_mappings(mappings, min_e, max_e);
  599. *answer = smartlist_join_strings(mappings, "\n", 1, NULL);
  600. SMARTLIST_FOREACH(mappings, char *, cp, tor_free(cp));
  601. smartlist_free(mappings);
  602. }
  603. return 0;
  604. }
  605. static int
  606. handle_control_getinfo(connection_t *conn, uint32_t len, const char *body)
  607. {
  608. smartlist_t *questions = NULL;
  609. smartlist_t *answers = NULL;
  610. char *msg = NULL, *ans;
  611. size_t msg_len;
  612. questions = smartlist_create();
  613. smartlist_split_string(questions, body, "\n",
  614. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  615. answers = smartlist_create();
  616. SMARTLIST_FOREACH(questions, const char *, q,
  617. {
  618. if (handle_getinfo_helper(q, &ans) < 0) {
  619. send_control_error(conn, ERR_INTERNAL, body);
  620. goto done;
  621. } if (!ans) {
  622. send_control_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
  623. goto done;
  624. }
  625. smartlist_add(answers, tor_strdup(q));
  626. smartlist_add(answers, ans);
  627. });
  628. msg = smartlist_join_strings2(answers, "\0", 1, 1, &msg_len);
  629. tor_assert(msg_len > 0); /* it will at least be terminated */
  630. send_control_message(conn, CONTROL_CMD_INFOVALUE,
  631. msg_len, msg);
  632. done:
  633. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  634. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  635. smartlist_free(answers);
  636. smartlist_free(questions);
  637. tor_free(msg);
  638. return 0;
  639. }
  640. static int
  641. handle_control_extendcircuit(connection_t *conn, uint32_t len,
  642. const char *body)
  643. {
  644. smartlist_t *router_nicknames, *routers;
  645. uint32_t circ_id;
  646. circuit_t *circ;
  647. char reply[4];
  648. if (len<5) {
  649. send_control_error(conn, ERR_SYNTAX, "extendcircuit message too short");
  650. return 0;
  651. }
  652. router_nicknames = smartlist_create();
  653. routers = smartlist_create();
  654. smartlist_split_string(router_nicknames, body+4, ",", 0, 0);
  655. SMARTLIST_FOREACH(router_nicknames, const char *, n,
  656. {
  657. routerinfo_t *r = router_get_by_nickname(n);
  658. if (!r) {
  659. send_control_error(conn, ERR_NO_ROUTER, n);
  660. goto done;
  661. }
  662. smartlist_add(routers, r);
  663. });
  664. if (!smartlist_len(routers)) {
  665. send_control_error(conn, ERR_SYNTAX, "No router names provided");
  666. goto done;
  667. }
  668. circ_id = ntohl(get_uint32(body));
  669. if (!circ_id) {
  670. /* start a new circuit */
  671. circ = circuit_init(CIRCUIT_PURPOSE_C_GENERAL, 0, 0, 0);
  672. } else {
  673. circ = circuit_get_by_global_id(circ_id);
  674. if (!circ) {
  675. send_control_error(conn, ERR_NO_CIRC,
  676. "No circuit found with given ID");
  677. goto done;
  678. }
  679. }
  680. /* now circ refers to something that is ready to be extended */
  681. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  682. {
  683. circuit_append_new_exit(circ, r);
  684. });
  685. /* now that we've populated the cpath, start extending */
  686. if (!circ_id) {
  687. if (circuit_handle_first_hop(circ) < 0) {
  688. circuit_mark_for_close(circ);
  689. send_control_error(conn, ERR_INTERNAL, "couldn't start circuit");
  690. goto done;
  691. }
  692. } else {
  693. if (circ->state == CIRCUIT_STATE_OPEN) {
  694. circ->state = CIRCUIT_STATE_BUILDING;
  695. if (circuit_send_next_onion_skin(circ) < 0) {
  696. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  697. circuit_mark_for_close(circ);
  698. send_control_error(conn, ERR_INTERNAL, "couldn't send onion skin");
  699. goto done;
  700. }
  701. }
  702. }
  703. set_uint32(reply, htonl(circ->global_identifier));
  704. send_control_done2(conn, reply, sizeof(reply));
  705. done:
  706. SMARTLIST_FOREACH(router_nicknames, char *, n, tor_free(n));
  707. smartlist_free(router_nicknames);
  708. smartlist_free(routers);
  709. return 0;
  710. }
  711. static int handle_control_attachstream(connection_t *conn, uint32_t len,
  712. const char *body)
  713. {
  714. uint32_t conn_id;
  715. uint32_t circ_id;
  716. connection_t *ap_conn;
  717. circuit_t *circ;
  718. if (len < 8) {
  719. send_control_error(conn, ERR_SYNTAX, "attachstream message too short");
  720. return 0;
  721. }
  722. conn_id = ntohl(get_uint32(body));
  723. circ_id = ntohl(get_uint32(body+4));
  724. if (!(ap_conn = connection_get_by_global_id(conn_id))) {
  725. send_control_error(conn, ERR_NO_STREAM,
  726. "No connection found with given ID");
  727. return 0;
  728. }
  729. if (ap_conn->state != AP_CONN_STATE_CONTROLLER_WAIT) {
  730. send_control_error(conn, ERR_NO_STREAM,
  731. "Connection was not managed by controller.");
  732. return 0;
  733. }
  734. if (!circ_id) {
  735. ap_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  736. if (connection_ap_handshake_attach_circuit(ap_conn)<0)
  737. connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_CANT_ATTACH);
  738. send_control_done(conn);
  739. return 0;
  740. }
  741. if (!(circ = circuit_get_by_global_id(circ_id))) {
  742. send_control_error(conn, ERR_NO_CIRC, "No circuit found with given ID");
  743. return 0;
  744. }
  745. if (circ->state != CIRCUIT_STATE_OPEN) {
  746. send_control_error(conn, ERR_INTERNAL, "Refuse to attach stream to non-open circ.");
  747. return 0;
  748. }
  749. if (connection_ap_handshake_attach_chosen_circuit(ap_conn, circ) != 1) {
  750. send_control_error(conn, ERR_INTERNAL, "Unable to attach stream.");
  751. return 0;
  752. }
  753. send_control_done(conn);
  754. return 0;
  755. }
  756. static int
  757. handle_control_postdescriptor(connection_t *conn, uint32_t len,
  758. const char *body)
  759. {
  760. const char *msg;
  761. switch (router_load_single_router(body, &msg)) {
  762. case -1:
  763. send_control_error(conn,ERR_SYNTAX,msg?msg: "Could not parse descriptor");
  764. break;
  765. case 0:
  766. send_control_done2(conn,msg?msg: "Descriptor not added",0);
  767. break;
  768. case 1:
  769. send_control_done(conn);
  770. return 0;
  771. }
  772. send_control_done(conn);
  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. int oldlog = EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE) &&
  1062. (severity == LOG_NOTICE || severity == LOG_WARN || severity == LOG_ERR);
  1063. int event = log_severity_to_event(severity);
  1064. if (event<0 || !EVENT_IS_INTERESTING(event))
  1065. event = 0;
  1066. if (oldlog || event) {
  1067. size_t len = strlen(msg);
  1068. if (event)
  1069. send_control_event(event, (uint32_t)(len+1), msg);
  1070. if (oldlog)
  1071. send_control_event(EVENT_LOG_OBSOLETE, (uint32_t)(len+1), msg);
  1072. }
  1073. }
  1074. /** Called whenever we receive new router descriptors: tell any
  1075. * interested control connections. <b>routers</b> is a list of
  1076. * DIGEST_LEN-byte identity digests.
  1077. */
  1078. int control_event_descriptors_changed(smartlist_t *routers)
  1079. {
  1080. size_t len;
  1081. char *msg;
  1082. smartlist_t *identities;
  1083. char buf[HEX_DIGEST_LEN+1];
  1084. if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
  1085. return 0;
  1086. identities = smartlist_create();
  1087. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1088. {
  1089. base16_encode(buf,sizeof(buf),r->identity_digest,DIGEST_LEN);
  1090. smartlist_add(identities, tor_strdup(buf));
  1091. });
  1092. msg = smartlist_join_strings(identities, ",", 1, &len);
  1093. send_control_event(EVENT_NEW_DESC, len+1, msg);
  1094. SMARTLIST_FOREACH(identities, char *, cp, tor_free(cp));
  1095. smartlist_free(identities);
  1096. tor_free(msg);
  1097. return 0;
  1098. }
  1099. /** Choose a random authentication cookie and write it to disk.
  1100. * Anybody who can read the cookie from disk will be considered
  1101. * authorized to use the control connection. */
  1102. int
  1103. init_cookie_authentication(int enabled)
  1104. {
  1105. char fname[512];
  1106. if (!enabled) {
  1107. authentication_cookie_is_set = 0;
  1108. return 0;
  1109. }
  1110. tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
  1111. get_options()->DataDirectory);
  1112. crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
  1113. authentication_cookie_is_set = 1;
  1114. if (write_bytes_to_file(fname, authentication_cookie,
  1115. AUTHENTICATION_COOKIE_LEN, 1)) {
  1116. log_fn(LOG_WARN,"Error writing authentication cookie.");
  1117. return -1;
  1118. }
  1119. return 0;
  1120. }