control.c 40 KB

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