control.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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. #define STATE_IS_OPEN(s) ((s) == CONTROL_CONN_STATE_OPEN_V0 || \
  11. (s) == CONTROL_CONN_STATE_OPEN_V1)
  12. #define STATE_IS_V0(s) ((s) == CONTROL_CONN_STATE_NEEDAUTH_V0 || \
  13. (s) == CONTROL_CONN_STATE_OPEN_V0)
  14. /*
  15. * See control-spec.txt and control-spec-v0.txt for full details on protocol(s).
  16. */
  17. /* Recognized message type codes. */
  18. #define CONTROL0_CMD_ERROR 0x0000
  19. #define CONTROL0_CMD_DONE 0x0001
  20. #define CONTROL0_CMD_SETCONF 0x0002
  21. #define CONTROL0_CMD_GETCONF 0x0003
  22. #define CONTROL0_CMD_CONFVALUE 0x0004
  23. #define CONTROL0_CMD_SETEVENTS 0x0005
  24. #define CONTROL0_CMD_EVENT 0x0006
  25. #define CONTROL0_CMD_AUTHENTICATE 0x0007
  26. #define CONTROL0_CMD_SAVECONF 0x0008
  27. #define CONTROL0_CMD_SIGNAL 0x0009
  28. #define CONTROL0_CMD_MAPADDRESS 0x000A
  29. #define CONTROL0_CMD_GETINFO 0x000B
  30. #define CONTROL0_CMD_INFOVALUE 0x000C
  31. #define CONTROL0_CMD_EXTENDCIRCUIT 0x000D
  32. #define CONTROL0_CMD_ATTACHSTREAM 0x000E
  33. #define CONTROL0_CMD_POSTDESCRIPTOR 0x000F
  34. #define CONTROL0_CMD_FRAGMENTHEADER 0x0010
  35. #define CONTROL0_CMD_FRAGMENT 0x0011
  36. #define CONTROL0_CMD_REDIRECTSTREAM 0x0012
  37. #define CONTROL0_CMD_CLOSESTREAM 0x0013
  38. #define CONTROL0_CMD_CLOSECIRCUIT 0x0014
  39. #define _CONTROL0_CMD_MAX_RECOGNIZED 0x0014
  40. /* Recognized error codes. */
  41. #define ERR_UNSPECIFIED 0x0000
  42. #define ERR_INTERNAL 0x0001
  43. #define ERR_UNRECOGNIZED_TYPE 0x0002
  44. #define ERR_SYNTAX 0x0003
  45. #define ERR_UNRECOGNIZED_CONFIG_KEY 0x0004
  46. #define ERR_INVALID_CONFIG_VALUE 0x0005
  47. #define ERR_UNRECOGNIZED_EVENT_CODE 0x0006
  48. #define ERR_UNAUTHORIZED 0x0007
  49. #define ERR_REJECTED_AUTHENTICATION 0x0008
  50. #define ERR_RESOURCE_EXHAUSETED 0x0009
  51. #define ERR_NO_STREAM 0x000A
  52. #define ERR_NO_CIRC 0x000B
  53. #define ERR_NO_ROUTER 0x000C
  54. /* Recognized asynchronous event types. */
  55. #define _EVENT_MIN 0x0001
  56. #define EVENT_CIRCUIT_STATUS 0x0001
  57. #define EVENT_STREAM_STATUS 0x0002
  58. #define EVENT_OR_CONN_STATUS 0x0003
  59. #define EVENT_BANDWIDTH_USED 0x0004
  60. #define EVENT_LOG_OBSOLETE 0x0005
  61. #define EVENT_NEW_DESC 0x0006
  62. #define EVENT_DEBUG_MSG 0x0007
  63. #define EVENT_INFO_MSG 0x0008
  64. #define EVENT_NOTICE_MSG 0x0009
  65. #define EVENT_WARN_MSG 0x000A
  66. #define EVENT_ERR_MSG 0x000B
  67. #define _EVENT_MAX 0x000B
  68. /** Array mapping from message type codes to human-readable message
  69. * type names. */
  70. static const char * CONTROL0_COMMANDS[_CONTROL0_CMD_MAX_RECOGNIZED+1] = {
  71. "error",
  72. "done",
  73. "setconf",
  74. "getconf",
  75. "confvalue",
  76. "setevents",
  77. "events",
  78. "authenticate",
  79. "saveconf",
  80. "signal",
  81. "mapaddress",
  82. "getinfo",
  83. "infovalue",
  84. "extendcircuit",
  85. "attachstream",
  86. "postdescriptor",
  87. "fragmentheader",
  88. "fragment",
  89. };
  90. /** Bitfield: The bit 1&lt;&lt;e is set if <b>any</b> open control
  91. * connection is interested in events of type <b>e</b>. We use this
  92. * so that we can decide to skip generating event messages that nobody
  93. * has interest in without having to walk over the global connection
  94. * list to find out.
  95. **/
  96. static uint32_t global_event_mask0 = 0;
  97. static uint32_t global_event_mask1 = 0;
  98. /** Macro: true if any control connection is interested in events of type
  99. * <b>e</b>. */
  100. #define EVENT_IS_INTERESTING0(e) (global_event_mask0 & (1<<(e)))
  101. #define EVENT_IS_INTERESTING1(e) (global_event_mask1 & (1<<(e)))
  102. #define EVENT_IS_INTERESTING(e) \
  103. ((global_event_mask0|global_event_mask1) & (1<<(e)))
  104. /** If we're using cookie-type authentication, how long should our cookies be?
  105. */
  106. #define AUTHENTICATION_COOKIE_LEN 32
  107. /** If true, we've set authentication_cookie to a secret code and
  108. * stored it to disk. */
  109. static int authentication_cookie_is_set = 0;
  110. static char authentication_cookie[AUTHENTICATION_COOKIE_LEN];
  111. static void connection_printf_to_buf(connection_t *conn, const char *format, ...)
  112. CHECK_PRINTF(2,3);
  113. static void update_global_event_mask(void);
  114. static void send_control0_message(connection_t *conn, uint16_t type,
  115. uint32_t len, const char *body);
  116. static void send_control_done(connection_t *conn);
  117. static void send_control_done2(connection_t *conn, const char *msg, size_t len);
  118. static void send_control0_error(connection_t *conn, uint16_t error,
  119. const char *message);
  120. static void send_control0_event(uint16_t event, uint32_t len, const char *body);
  121. static void send_control1_event(uint16_t event, const char *format, ...)
  122. CHECK_PRINTF(2,3);
  123. static int handle_control_setconf(connection_t *conn, uint32_t len,
  124. char *body);
  125. static int handle_control_getconf(connection_t *conn, uint32_t len,
  126. const char *body);
  127. static int handle_control_setevents(connection_t *conn, uint32_t len,
  128. const char *body);
  129. static int handle_control_authenticate(connection_t *conn, uint32_t len,
  130. const char *body);
  131. static int handle_control_saveconf(connection_t *conn, uint32_t len,
  132. const char *body);
  133. static int handle_control_signal(connection_t *conn, uint32_t len,
  134. const char *body);
  135. static int handle_control_mapaddress(connection_t *conn, uint32_t len,
  136. const char *body);
  137. static int handle_control_getinfo(connection_t *conn, uint32_t len,
  138. const char *body);
  139. static int handle_control_extendcircuit(connection_t *conn, uint32_t len,
  140. const char *body);
  141. static int handle_control_attachstream(connection_t *conn, uint32_t len,
  142. const char *body);
  143. static int handle_control_postdescriptor(connection_t *conn, uint32_t len,
  144. const char *body);
  145. static int handle_control_redirectstream(connection_t *conn, uint32_t len,
  146. const char *body);
  147. static int handle_control_closestream(connection_t *conn, uint32_t len,
  148. const char *body);
  149. static int handle_control_closecircuit(connection_t *conn, uint32_t len,
  150. const char *body);
  151. /** Given a possibly invalid message type code <b>cmd</b>, return a
  152. * human-readable string equivalent. */
  153. static INLINE const char *
  154. control_cmd_to_string(uint16_t cmd)
  155. {
  156. return (cmd<=_CONTROL0_CMD_MAX_RECOGNIZED) ? CONTROL0_COMMANDS[cmd] : "Unknown";
  157. }
  158. static INLINE int
  159. event_to_log_severity(int event)
  160. {
  161. switch (event) {
  162. case EVENT_DEBUG_MSG: return LOG_DEBUG;
  163. case EVENT_INFO_MSG: return LOG_INFO;
  164. case EVENT_NOTICE_MSG: return LOG_NOTICE;
  165. case EVENT_WARN_MSG: return LOG_WARN;
  166. case EVENT_ERR_MSG: return LOG_ERR;
  167. default: return -1;
  168. }
  169. }
  170. /** DOCDOC */
  171. static INLINE int
  172. log_severity_to_event(int severity)
  173. {
  174. switch (severity) {
  175. case LOG_DEBUG: return EVENT_DEBUG_MSG;
  176. case LOG_INFO: return EVENT_INFO_MSG;
  177. case LOG_NOTICE: return EVENT_NOTICE_MSG;
  178. case LOG_WARN: return EVENT_WARN_MSG;
  179. case LOG_ERR: return EVENT_ERR_MSG;
  180. default: return -1;
  181. }
  182. }
  183. /** Set <b>global_event_mask</b> to the bitwise OR of each live control
  184. * connection's event_mask field. */
  185. static void
  186. update_global_event_mask(void)
  187. {
  188. connection_t **conns;
  189. int n_conns, i;
  190. global_event_mask0 = 0;
  191. global_event_mask1 = 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. STATE_IS_OPEN(conns[i]->state)) {
  196. if (STATE_IS_V0(conns[i]->state))
  197. global_event_mask0 |= conns[i]->event_mask;
  198. else
  199. global_event_mask1 |= conns[i]->event_mask;
  200. }
  201. }
  202. adjust_event_log_severity();
  203. }
  204. /** DOCDOC */
  205. void
  206. adjust_event_log_severity(void)
  207. {
  208. int i;
  209. int min_log_event=EVENT_ERR_MSG, max_log_event=EVENT_DEBUG_MSG;
  210. for (i = EVENT_DEBUG_MSG; i <= EVENT_ERR_MSG; ++i) {
  211. if (EVENT_IS_INTERESTING(i)) {
  212. min_log_event = i;
  213. break;
  214. }
  215. }
  216. for (i = EVENT_ERR_MSG; i >= EVENT_DEBUG_MSG; --i) {
  217. if (EVENT_IS_INTERESTING(i)) {
  218. max_log_event = i;
  219. break;
  220. }
  221. }
  222. if (EVENT_IS_INTERESTING(EVENT_LOG_OBSOLETE)) {
  223. if (min_log_event > EVENT_NOTICE_MSG)
  224. min_log_event = EVENT_NOTICE_MSG;
  225. if (max_log_event < EVENT_ERR_MSG)
  226. max_log_event = EVENT_ERR_MSG;
  227. }
  228. change_callback_log_severity(event_to_log_severity(min_log_event),
  229. event_to_log_severity(max_log_event),
  230. control_event_logmsg);
  231. }
  232. static INLINE void
  233. connection_write_str_to_buf(const char *s, connection_t *conn)
  234. {
  235. size_t len = strlen(s);
  236. connection_write_to_buf(s, len, conn);
  237. }
  238. static size_t
  239. write_escaped_data(const char *data, size_t len, int translate_newlines,
  240. char **out)
  241. {
  242. size_t sz_out = len+8;
  243. char *outp;
  244. const char *end;
  245. int i;
  246. int start_of_line;
  247. for (i=0; i<len; ++i) {
  248. if (data[i]== '\n')
  249. ++sz_out;
  250. }
  251. *out = outp = tor_malloc(sz_out+1);
  252. end = data+len;
  253. start_of_line = 1;
  254. while (data < end) {
  255. if (*data == '\n') {
  256. if (translate_newlines)
  257. *outp++ = '\r';
  258. start_of_line = 1;
  259. } else if (*data == '.') {
  260. if (start_of_line) {
  261. start_of_line = 0;
  262. *outp++ = '.';
  263. }
  264. } else {
  265. start_of_line = 0;
  266. }
  267. *outp++ = *data++;
  268. }
  269. *outp++ = '\r';
  270. *outp++ = '\n';
  271. *outp++ = '.';
  272. *outp++ = '\r';
  273. *outp++ = '\n';
  274. return outp - *out;
  275. }
  276. #if 0
  277. static void
  278. connection_write_escaped_data_to_buf(const char *data, size_t len,
  279. int translate_newlines,
  280. connection_t *conn)
  281. {
  282. const char *next;
  283. while (len) {
  284. if (*data == '.')
  285. connection_write_to_buf(".", 1, conn);
  286. if (translate_newlines)
  287. next = tor_memmem(data, len, "\r\n", 2);
  288. else
  289. next = tor_memmem(data, len, "\r\n.", 3);
  290. if (next) {
  291. if (translate_newlines) {
  292. connection_write_to_buf(data, next-data, conn);
  293. connection_write_to_buf("\n", 1, conn);
  294. len -= (next-data+2);
  295. } else {
  296. connection_write_to_buf(data, next-data+2, conn);
  297. len -= (next-data+2);
  298. }
  299. data = next + 2;
  300. } else {
  301. connection_write_to_buf(data, len, conn);
  302. break;
  303. }
  304. }
  305. connection_write_to_buf(".\r\n", 3, conn);
  306. }
  307. #endif
  308. static size_t
  309. read_escaped_data(const char *data, size_t len, int translate_newlines,
  310. char **out)
  311. {
  312. char *outp;
  313. const char *next;
  314. *out = outp = tor_malloc(len);
  315. while (len) {
  316. if (*data == '.')
  317. ++data;
  318. if (translate_newlines)
  319. next = tor_memmem(data, len, "\r\n", 2);
  320. else
  321. next = tor_memmem(data, len, "\r\n.", 3);
  322. if (next) {
  323. memcpy(outp, data, next-data);
  324. outp += (next-data);
  325. data = next+2;
  326. } else {
  327. memcpy(outp, data, len);
  328. outp += len;
  329. return outp - *out;
  330. }
  331. if (translate_newlines) {
  332. *outp++ = '\n';
  333. } else {
  334. *outp++ = '\r';
  335. *outp++ = '\n';
  336. }
  337. }
  338. return outp - *out;
  339. }
  340. static void
  341. connection_printf_to_buf(connection_t *conn, const char *format, ...)
  342. {
  343. va_list ap;
  344. char buf[1024];
  345. int r;
  346. size_t len;
  347. va_start(ap,format);
  348. r = tor_vsnprintf(buf, sizeof(buf), format, ap);
  349. va_end(ap);
  350. len = strlen(buf);
  351. if (memcmp("\r\n\0", buf+len-2, 3)) {
  352. buf[1023] = '\0';
  353. buf[1022] = '\n';
  354. buf[1021] = '\r';
  355. }
  356. connection_write_to_buf(buf, len, conn);
  357. }
  358. #if 0
  359. static void
  360. connection_write_reply_lines_to_buf(connection_t *conn,
  361. const char *code, smartlist_t *lines)
  362. {
  363. int i, len;
  364. tor_assert(strlen(code) == 3);
  365. len = smartlist_len(lines);
  366. if (!len)
  367. return;
  368. for (i=0; i < len-1; ++i) {
  369. connection_write_to_buf(code, 3, conn);
  370. connection_write_to_buf("-", 1, conn);
  371. connection_write_str_to_buf(smartlist_get(lines, i), conn);
  372. }
  373. connection_write_to_buf(code, 3, conn);
  374. connection_write_to_buf(" ", 1, conn);
  375. connection_write_str_to_buf(smartlist_get(lines, len-1), conn);
  376. }
  377. #endif
  378. /** Send a message of type <b>type</b> containing <b>len</b> bytes
  379. * from <b>body</b> along the control connection <b>conn</b> */
  380. static void
  381. send_control0_message(connection_t *conn, uint16_t type, uint32_t len,
  382. const char *body)
  383. {
  384. char buf[10];
  385. tor_assert(conn);
  386. tor_assert(STATE_IS_V0(conn->state));
  387. tor_assert(len || !body);
  388. tor_assert(type <= _CONTROL0_CMD_MAX_RECOGNIZED);
  389. if (len < 65536) {
  390. set_uint16(buf, htons(len));
  391. set_uint16(buf+2, htons(type));
  392. connection_write_to_buf(buf, 4, conn);
  393. if (len)
  394. connection_write_to_buf(body, len, conn);
  395. } else {
  396. set_uint16(buf, htons(65535));
  397. set_uint16(buf+2, htons(CONTROL0_CMD_FRAGMENTHEADER));
  398. set_uint16(buf+4, htons(type));
  399. set_uint32(buf+6, htonl(len));
  400. connection_write_to_buf(buf, 10, conn);
  401. connection_write_to_buf(body, 65535-6, conn);
  402. len -= (65535-6);
  403. body += (65535-6);
  404. while (len) {
  405. size_t chunklen = (len<65535)?len:65535;
  406. set_uint16(buf, htons((uint16_t)chunklen));
  407. set_uint16(buf+2, htons(CONTROL0_CMD_FRAGMENT));
  408. connection_write_to_buf(buf, 4, conn);
  409. connection_write_to_buf(body, chunklen, conn);
  410. len -= chunklen;
  411. body += chunklen;
  412. }
  413. }
  414. }
  415. /** Send a "DONE" message down the control connection <b>conn</b> */
  416. static void
  417. send_control_done(connection_t *conn)
  418. {
  419. if (STATE_IS_V0(conn->state)) {
  420. send_control0_message(conn, CONTROL0_CMD_DONE, 0, NULL);
  421. } else {
  422. connection_write_str_to_buf("250 OK\r\n", conn);
  423. }
  424. }
  425. /** DOCDOC */
  426. static void
  427. send_control_done2(connection_t *conn, const char *msg, size_t len)
  428. {
  429. if (len==0)
  430. len = strlen(msg);
  431. send_control0_message(conn, CONTROL0_CMD_DONE, len, msg);
  432. }
  433. /** Send an error message with error code <b>error</b> and body
  434. * <b>message</b> down the connection <b>conn</b> */
  435. static void
  436. send_control0_error(connection_t *conn, uint16_t error, const char *message)
  437. {
  438. char buf[256];
  439. size_t len;
  440. set_uint16(buf, htons(error));
  441. len = strlen(message);
  442. tor_assert(len < (256-2));
  443. memcpy(buf+2, message, len);
  444. send_control0_message(conn, CONTROL0_CMD_ERROR, (uint16_t)(len+2), buf);
  445. }
  446. /** Send an 'event' message of event type <b>event</b>, containing
  447. * <b>len</b> bytes in <b>body</b> to every control connection that
  448. * is interested in it. */
  449. static void
  450. send_control0_event(uint16_t event, uint32_t len, const char *body)
  451. {
  452. connection_t **conns;
  453. int n_conns, i;
  454. size_t buflen;
  455. char *buf;
  456. tor_assert(event >= _EVENT_MIN && event <= _EVENT_MAX);
  457. buflen = len + 2;
  458. buf = tor_malloc_zero(buflen);
  459. set_uint16(buf, htons(event));
  460. memcpy(buf+2, body, len);
  461. get_connection_array(&conns, &n_conns);
  462. for (i = 0; i < n_conns; ++i) {
  463. if (conns[i]->type == CONN_TYPE_CONTROL &&
  464. conns[i]->state == CONTROL_CONN_STATE_OPEN_V0 &&
  465. conns[i]->event_mask & (1<<event)) {
  466. send_control0_message(conns[i], CONTROL0_CMD_EVENT, buflen, buf);
  467. if (event == EVENT_ERR_MSG) {
  468. _connection_controller_force_write(conns[i]);
  469. }
  470. }
  471. }
  472. tor_free(buf);
  473. }
  474. static void
  475. send_control1_event(uint16_t event, const char *format, ...)
  476. {
  477. connection_t **conns;
  478. int n_conns, i, r;
  479. char buf[1024]; /* XXXX Length */
  480. va_list ap;
  481. size_t len;
  482. va_start(ap, format);
  483. r = tor_vsnprintf(buf, sizeof(buf), format, ap);
  484. va_end(ap);
  485. len = strlen(buf);
  486. if (memcmp("\r\n\0", buf+len-2, 3)) {
  487. buf[1023] = '\0';
  488. buf[1022] = '\n';
  489. buf[1021] = '\r';
  490. }
  491. tor_assert(event >= _EVENT_MIN && event <= _EVENT_MAX);
  492. get_connection_array(&conns, &n_conns);
  493. for (i = 0; i < n_conns; ++i) {
  494. if (conns[i]->type == CONN_TYPE_CONTROL &&
  495. conns[i]->state == CONTROL_CONN_STATE_OPEN_V1 &&
  496. conns[i]->event_mask & (1<<event)) {
  497. connection_write_to_buf(buf, len, conns[i]);
  498. if (event == EVENT_ERR_MSG) {
  499. _connection_controller_force_write(conns[i]);
  500. }
  501. }
  502. }
  503. }
  504. static circuit_t *
  505. get_circ(const char *id)
  506. {
  507. unsigned long n_id;
  508. int ok;
  509. n_id = tor_parse_ulong(id, 10, 0, ULONG_MAX, &ok, NULL);
  510. if (!ok)
  511. return NULL;
  512. return circuit_get_by_global_id(n_id);
  513. }
  514. static connection_t *
  515. get_stream(const char *id)
  516. {
  517. unsigned long n_id;
  518. int ok;
  519. connection_t *conn;
  520. n_id = tor_parse_ulong(id, 10, 0, ULONG_MAX, &ok, NULL);
  521. if (!ok)
  522. return NULL;
  523. conn = connection_get_by_global_id(n_id);
  524. if (conn->type != CONN_TYPE_AP)
  525. return NULL;
  526. return conn;
  527. }
  528. /** Called when we receive a SETCONF message: parse the body and try
  529. * to update our configuration. Reply with a DONE or ERROR message. */
  530. static int
  531. handle_control_setconf(connection_t *conn, uint32_t len, char *body)
  532. {
  533. /* XXXX V1 */
  534. int r;
  535. struct config_line_t *lines=NULL;
  536. if (config_get_lines(body, &lines) < 0) {
  537. log_fn(LOG_WARN,"Controller gave us config lines we can't parse.");
  538. send_control0_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
  539. return 0;
  540. }
  541. if ((r=config_trial_assign(lines, 1)) < 0) {
  542. log_fn(LOG_WARN,"Controller gave us config lines that didn't validate.");
  543. if (r==-1) {
  544. send_control0_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY,
  545. "Unrecognized option");
  546. } else {
  547. send_control0_error(conn, ERR_INVALID_CONFIG_VALUE,"Invalid option value");
  548. }
  549. config_free_lines(lines);
  550. return 0;
  551. }
  552. config_free_lines(lines);
  553. if (options_act() < 0) { /* acting on them failed. die. */
  554. log_fn(LOG_ERR,"Acting on config options left us in a broken state. Dying.");
  555. exit(1);
  556. }
  557. send_control_done(conn);
  558. return 0;
  559. }
  560. /** Called when we receive a GETCONF message. Parse the request, and
  561. * reply with a CONFVALUE or an ERROR message */
  562. static int
  563. handle_control_getconf(connection_t *conn, uint32_t body_len, const char *body)
  564. {
  565. smartlist_t *questions = NULL;
  566. smartlist_t *answers = NULL;
  567. smartlist_t *unrecognized = NULL;
  568. char *msg = NULL;
  569. size_t msg_len;
  570. or_options_t *options = get_options();
  571. int v0 = STATE_IS_V0(conn->state);
  572. questions = smartlist_create();
  573. if (v0) {
  574. smartlist_split_string(questions, body, "\n",
  575. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  576. } else {
  577. smartlist_split_string(questions, body, " ",
  578. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  579. }
  580. answers = smartlist_create();
  581. unrecognized = smartlist_create();
  582. SMARTLIST_FOREACH(questions, char *, q,
  583. {
  584. if (!config_option_is_recognized(q)) {
  585. if (v0) {
  586. send_control0_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, q);
  587. goto done;
  588. } else {
  589. smartlist_add(unrecognized, q);
  590. }
  591. } else {
  592. struct config_line_t *answer = config_get_assigned_option(options,q);
  593. while (answer) {
  594. struct config_line_t *next;
  595. size_t alen = strlen(answer->key)+strlen(answer->value)+8;
  596. char *astr = tor_malloc(alen);
  597. if (v0)
  598. tor_snprintf(astr, alen, "%s %s\n", answer->key, answer->value);
  599. else
  600. tor_snprintf(astr, alen, "250-%s=%s\r\n", answer->key, answer->value);
  601. smartlist_add(answers, astr);
  602. next = answer->next;
  603. tor_free(answer->key);
  604. tor_free(answer->value);
  605. tor_free(answer);
  606. answer = next;
  607. }
  608. }
  609. });
  610. if (v0) {
  611. msg = smartlist_join_strings(answers, "", 0, &msg_len);
  612. send_control0_message(conn, CONTROL0_CMD_CONFVALUE,
  613. (uint16_t)msg_len, msg_len?msg:NULL);
  614. } else {
  615. int i,len;
  616. if ((len = smartlist_len(unrecognized))) {
  617. for (i=0; i < len-1; ++i)
  618. connection_printf_to_buf(conn,
  619. "552-Unrecognized configuration key \"%s\"\r\n",
  620. (char*)smartlist_get(unrecognized, i));
  621. connection_printf_to_buf(conn,
  622. "552 Unrecognized configuration key \"%s\"\r\n",
  623. (char*)smartlist_get(unrecognized, len-1));
  624. } else if ((len = smartlist_len(answers))) {
  625. char *tmp = smartlist_get(answers, len-1);
  626. tor_assert(strlen(tmp)>4);
  627. tmp[3] = ' ';
  628. msg = smartlist_join_strings(answers, "", 0, &msg_len);
  629. connection_write_to_buf(msg, msg_len, conn);
  630. } else {
  631. connection_write_str_to_buf("250 OK\r\n", conn);
  632. }
  633. }
  634. done:
  635. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  636. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  637. smartlist_free(answers);
  638. smartlist_free(questions);
  639. smartlist_free(unrecognized);
  640. tor_free(msg);
  641. return 0;
  642. }
  643. /** Called when we get a SETEVENTS message: update conn->event_mask,
  644. * and reply with DONE or ERROR. */
  645. static int
  646. handle_control_setevents(connection_t *conn, uint32_t len, const char *body)
  647. {
  648. uint16_t event_code;
  649. uint32_t event_mask = 0;
  650. if (STATE_IS_V0(conn->state)) {
  651. if (len % 2) {
  652. send_control0_error(conn, ERR_SYNTAX,
  653. "Odd number of bytes in setevents message");
  654. return 0;
  655. }
  656. for (; len; len -= 2, body += 2) {
  657. event_code = ntohs(get_uint16(body));
  658. if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
  659. send_control0_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
  660. "Unrecognized event code");
  661. return 0;
  662. }
  663. event_mask |= (1 << event_code);
  664. }
  665. } else {
  666. smartlist_t *events = smartlist_create();
  667. smartlist_split_string(events, body, " ",
  668. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  669. SMARTLIST_FOREACH(events, const char *, ev,
  670. {
  671. if (!strcasecmp(ev, "CIRC"))
  672. event_code = EVENT_CIRCUIT_STATUS;
  673. else if (!strcasecmp(ev, "STREAM"))
  674. event_code = EVENT_STREAM_STATUS;
  675. else if (!strcasecmp(ev, "ORCONN"))
  676. event_code = EVENT_OR_CONN_STATUS;
  677. else if (!strcasecmp(ev, "BW"))
  678. event_code = EVENT_BANDWIDTH_USED;
  679. else if (!strcasecmp(ev, "DEBUG"))
  680. continue; /* Don't do debug messages */
  681. else if (!strcasecmp(ev, "INFO"))
  682. event_code = EVENT_INFO_MSG;
  683. else if (!strcasecmp(ev, "NOTICE"))
  684. event_code = EVENT_NOTICE_MSG;
  685. else if (!strcasecmp(ev, "WARN"))
  686. event_code = EVENT_WARN_MSG;
  687. else if (!strcasecmp(ev, "ERR"))
  688. event_code = EVENT_ERR_MSG;
  689. else if (!strcasecmp(ev, "NEWDESC"))
  690. event_code = EVENT_NEW_DESC;
  691. else {
  692. connection_printf_to_buf(conn, "552 Unrecognized event \"%s\"\r\n",
  693. ev);
  694. SMARTLIST_FOREACH(events, char *, e, tor_free(e));
  695. smartlist_free(events);
  696. return 0;
  697. }
  698. event_mask |= (1 << event_code);
  699. });
  700. SMARTLIST_FOREACH(events, char *, e, tor_free(e));
  701. smartlist_free(events);
  702. }
  703. conn->event_mask = event_mask;
  704. update_global_event_mask();
  705. send_control_done(conn);
  706. return 0;
  707. }
  708. /** Decode the hashed, base64'd password stored in <b>hashed</b>. If
  709. * <b>buf</b> is provided, store the hashed password in the first
  710. * S2K_SPECIFIER_LEN+DIGEST_LEN bytes of <b>buf</b>. Return 0 on
  711. * success, -1 on failure.
  712. */
  713. int
  714. decode_hashed_password(char *buf, const char *hashed)
  715. {
  716. char decoded[64];
  717. if (!strcmpstart(hashed, "16:")) {
  718. if (base16_decode(decoded, sizeof(decoded), hashed+3, strlen(hashed+3))<0
  719. || strlen(hashed+3) != (S2K_SPECIFIER_LEN+DIGEST_LEN)*2) {
  720. return -1;
  721. }
  722. } else {
  723. if (base64_decode(decoded, sizeof(decoded), hashed, strlen(hashed))
  724. != S2K_SPECIFIER_LEN+DIGEST_LEN) {
  725. return -1;
  726. }
  727. }
  728. if (buf)
  729. memcpy(buf, decoded, S2K_SPECIFIER_LEN+DIGEST_LEN);
  730. return 0;
  731. }
  732. /** Called when we get an AUTHENTICATE message. Check whether the
  733. * authentication is valid, and if so, update the connection's state to
  734. * OPEN. Reply with DONE or ERROR.
  735. */
  736. static int
  737. handle_control_authenticate(connection_t *conn, uint32_t len, const char *body)
  738. {
  739. /* XXXX V1 */
  740. or_options_t *options = get_options();
  741. if (options->CookieAuthentication) {
  742. if (len == AUTHENTICATION_COOKIE_LEN &&
  743. !memcmp(authentication_cookie, body, len)) {
  744. goto ok;
  745. }
  746. } else if (options->HashedControlPassword) {
  747. char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
  748. char received[DIGEST_LEN];
  749. if (decode_hashed_password(expected, options->HashedControlPassword)<0) {
  750. log_fn(LOG_WARN,"Couldn't decode HashedControlPassword: invalid base64");
  751. goto err;
  752. }
  753. secret_to_key(received,DIGEST_LEN,body,len,expected);
  754. if (!memcmp(expected+S2K_SPECIFIER_LEN, received, DIGEST_LEN))
  755. goto ok;
  756. goto err;
  757. } else {
  758. /* if Tor doesn't demand any stronger authentication, then
  759. * the controller can get in with anything. */
  760. goto ok;
  761. }
  762. err:
  763. if (STATE_IS_V0(conn->state))
  764. send_control0_error(conn,ERR_REJECTED_AUTHENTICATION,"Authentication failed");
  765. else
  766. connection_write_str_to_buf("515 Authentication failed\r\n", conn);
  767. return 0;
  768. ok:
  769. log_fn(LOG_INFO, "Authenticated control connection (%d)", conn->s);
  770. send_control_done(conn);
  771. if (STATE_IS_V0(conn->state))
  772. conn->state = CONTROL_CONN_STATE_OPEN_V0;
  773. else
  774. conn->state = CONTROL_CONN_STATE_OPEN_V1;
  775. return 0;
  776. }
  777. /** DOCDOC */
  778. static int
  779. handle_control_saveconf(connection_t *conn, uint32_t len,
  780. const char *body)
  781. {
  782. if (save_current_config()<0) {
  783. if (STATE_IS_V0(conn->state))
  784. send_control0_error(conn, ERR_INTERNAL,
  785. "Unable to write configuration to disk.");
  786. else
  787. connection_write_str_to_buf("551 Unable to write configuration to disk.",
  788. conn);
  789. } else {
  790. send_control_done(conn);
  791. }
  792. return 0;
  793. }
  794. /** DOCDOC */
  795. static int
  796. handle_control_signal(connection_t *conn, uint32_t len,
  797. const char *body)
  798. {
  799. int sig;
  800. if (STATE_IS_V0(conn->state)) {
  801. if (len != 1) {
  802. send_control0_error(conn, ERR_SYNTAX,
  803. "Body of SIGNAL command too long or too short.");
  804. return 0;
  805. } else {
  806. sig = (uint8_t)body[0];
  807. }
  808. } else {
  809. int n = 0;
  810. char *s;
  811. while (body[n] && ! TOR_ISSPACE(body[n]))
  812. ++n;
  813. s = tor_strndup(body, n);
  814. if (!strcasecmp(s, "RELOAD") || !strcasecmp(s, "HUP"))
  815. sig = SIGHUP;
  816. else if (!strcasecmp(s, "SHUTDOWN") || !strcasecmp(s, "INT"))
  817. sig = SIGINT;
  818. else if (!strcasecmp(s, "DUMP") || !strcasecmp(s, "USR1"))
  819. sig = SIGUSR1;
  820. else if (!strcasecmp(s, "DEBUG") || !strcasecmp(s, "USR2"))
  821. sig = SIGUSR2;
  822. else if (!strcasecmp(s, "HALT") || !strcasecmp(s, "TERM"))
  823. sig = SIGTERM;
  824. else {
  825. connection_printf_to_buf(conn, "552 Unrecognized signal code \"%s\"\r\n",
  826. body);
  827. sig = -1;
  828. }
  829. tor_free(s);
  830. if (sig<0)
  831. return 0;
  832. }
  833. if (control_signal_act(sig) < 0) {
  834. if (STATE_IS_V0(conn->state))
  835. send_control0_error(conn, ERR_SYNTAX, "Unrecognized signal number.");
  836. else
  837. connection_write_str_to_buf("551 Internal error acting on signal\r\n",
  838. conn);
  839. } else {
  840. send_control_done(conn);
  841. }
  842. return 0;
  843. }
  844. /** DOCDOC */
  845. static int
  846. handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body)
  847. {
  848. /* XXXX V1 */
  849. smartlist_t *elts;
  850. smartlist_t *lines;
  851. smartlist_t *reply;
  852. char *r;
  853. size_t sz;
  854. lines = smartlist_create();
  855. elts = smartlist_create();
  856. reply = smartlist_create();
  857. smartlist_split_string(lines, body, "\n",
  858. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  859. SMARTLIST_FOREACH(lines, char *, line,
  860. {
  861. tor_strlower(line);
  862. smartlist_split_string(elts, line, " ", 0, 2);
  863. if (smartlist_len(elts) == 2) {
  864. const char *from = smartlist_get(elts,0);
  865. const char *to = smartlist_get(elts,1);
  866. if (!is_plausible_address(from)) {
  867. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",from);
  868. } else if (!is_plausible_address(to)) {
  869. log_fn(LOG_WARN,"Skipping invalid argument '%s' in MapAddress msg",to);
  870. } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
  871. const char *addr = addressmap_register_virtual_address(
  872. strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
  873. tor_strdup(to));
  874. if (!addr) {
  875. log_fn(LOG_WARN,
  876. "Unable to allocate address for '%s' in MapAddress msg",
  877. safe_str(line));
  878. } else {
  879. size_t anslen = strlen(addr)+strlen(to)+2;
  880. char *ans = tor_malloc(anslen);
  881. tor_snprintf(ans, anslen, "%s %s", addr, to);
  882. smartlist_add(reply, ans);
  883. }
  884. } else {
  885. addressmap_register(from, tor_strdup(to), 1);
  886. smartlist_add(reply, tor_strdup(line));
  887. }
  888. } else {
  889. log_fn(LOG_WARN, "Skipping MapAddress line with wrong number of items.");
  890. }
  891. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  892. smartlist_clear(elts);
  893. });
  894. SMARTLIST_FOREACH(lines, char *, cp, tor_free(cp));
  895. smartlist_free(lines);
  896. smartlist_free(elts);
  897. r = smartlist_join_strings(reply, "\n", 1, &sz);
  898. send_control_done2(conn,r,sz);
  899. SMARTLIST_FOREACH(reply, char *, cp, tor_free(cp));
  900. smartlist_free(reply);
  901. tor_free(r);
  902. return 0;
  903. }
  904. /** Lookup the 'getinfo' entry <b>question</b>, and return
  905. * the answer in <b>*answer</b> (or NULL if key not recognized).
  906. * Return 0 if success, or -1 if internal error. */
  907. static int
  908. handle_getinfo_helper(const char *question, char **answer)
  909. {
  910. *answer = NULL; /* unrecognized key by default */
  911. if (!strcmp(question, "version")) {
  912. *answer = tor_strdup(VERSION);
  913. } else if (!strcmpstart(question, "desc/id/")) {
  914. routerinfo_t *ri = router_get_by_hexdigest(question+strlen("desc/id/"));
  915. if (ri && ri->signed_descriptor)
  916. *answer = tor_strdup(ri->signed_descriptor);
  917. } else if (!strcmpstart(question, "desc/name/")) {
  918. routerinfo_t *ri = router_get_by_nickname(question+strlen("desc/name/"));
  919. if (ri && ri->signed_descriptor)
  920. *answer = tor_strdup(ri->signed_descriptor);
  921. } else if (!strcmp(question, "network-status")) {
  922. routerlist_t *routerlist;
  923. router_get_routerlist(&routerlist);
  924. if (!routerlist || !routerlist->routers ||
  925. list_server_status(routerlist->routers, answer) < 0) {
  926. return -1;
  927. }
  928. } else if (!strcmpstart(question, "addr-mappings/")) {
  929. time_t min_e, max_e;
  930. smartlist_t *mappings;
  931. if (!strcmp(question, "addr-mappings/all")) {
  932. min_e = 0; max_e = TIME_MAX;
  933. } else if (!strcmp(question, "addr-mappings/cache")) {
  934. min_e = 2; max_e = TIME_MAX;
  935. } else if (!strcmp(question, "addr-mappings/config")) {
  936. min_e = 0; max_e = 0;
  937. } else if (!strcmp(question, "addr-mappings/control")) {
  938. min_e = 1; max_e = 1;
  939. } else {
  940. return 0;
  941. }
  942. mappings = smartlist_create();
  943. addressmap_get_mappings(mappings, min_e, max_e);
  944. *answer = smartlist_join_strings(mappings, "\n", 1, NULL);
  945. SMARTLIST_FOREACH(mappings, char *, cp, tor_free(cp));
  946. smartlist_free(mappings);
  947. }
  948. return 0;
  949. }
  950. /** DOCDOC */
  951. static int
  952. handle_control_getinfo(connection_t *conn, uint32_t len, const char *body)
  953. {
  954. smartlist_t *questions = NULL;
  955. smartlist_t *answers = NULL;
  956. smartlist_t *unrecognized = NULL;
  957. char *msg = NULL, *ans;
  958. size_t msg_len;
  959. int v0 = STATE_IS_V0(conn->state);
  960. questions = smartlist_create();
  961. if (v0)
  962. smartlist_split_string(questions, body, "\n",
  963. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  964. else
  965. smartlist_split_string(questions, body, " ",
  966. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  967. answers = smartlist_create();
  968. unrecognized = smartlist_create();
  969. SMARTLIST_FOREACH(questions, const char *, q,
  970. {
  971. if (handle_getinfo_helper(q, &ans) < 0) {
  972. if (v0)
  973. send_control0_error(conn, ERR_INTERNAL, body);
  974. else
  975. connection_write_str_to_buf("551 Internal error\r\n", conn);
  976. goto done;
  977. }
  978. if (!ans) {
  979. if (v0) {
  980. send_control0_error(conn, ERR_UNRECOGNIZED_CONFIG_KEY, body);
  981. goto done;
  982. } else
  983. smartlist_add(unrecognized, (char*)q);
  984. } else {
  985. smartlist_add(answers, tor_strdup(q));
  986. smartlist_add(answers, ans);
  987. }
  988. });
  989. if (smartlist_len(unrecognized)) {
  990. int i;
  991. tor_assert(!v0);
  992. for (i=0; i < len-1; ++i)
  993. connection_printf_to_buf(conn,
  994. "552-Unrecognized configuration key \"%s\"\r\n",
  995. (char*)smartlist_get(unrecognized, i));
  996. connection_printf_to_buf(conn,
  997. "552 Unrecognized configuration key \"%s\"\r\n",
  998. (char*)smartlist_get(unrecognized, len-1));
  999. goto done;
  1000. }
  1001. if (v0) {
  1002. msg = smartlist_join_strings2(answers, "\0", 1, 1, &msg_len);
  1003. tor_assert(msg_len > 0); /* it will at least be terminated */
  1004. send_control0_message(conn, CONTROL0_CMD_INFOVALUE,
  1005. msg_len, msg);
  1006. } else if (smartlist_len(answers)) {
  1007. int i;
  1008. for (i = 0; i < smartlist_len(answers); i += 2) {
  1009. char *k = smartlist_get(answers, i);
  1010. char *v = smartlist_get(answers, i+1);
  1011. /*XXXX Not an adequate test! XXXX011 */
  1012. if (!strchr(v, '\n') && !strchr(v, '\r')) {
  1013. connection_printf_to_buf(conn, "250-%s=%s\r\n", k, v);
  1014. } else {
  1015. char *esc = NULL;
  1016. size_t len;
  1017. len = write_escaped_data(v, strlen(v), 1, &esc);
  1018. connection_printf_to_buf(conn, "250+%s=", k);
  1019. connection_write_to_buf(esc, len, conn);
  1020. tor_free(esc);
  1021. }
  1022. }
  1023. connection_write_str_to_buf("250 OK\r\n", conn);
  1024. }
  1025. done:
  1026. if (answers) SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
  1027. if (questions) SMARTLIST_FOREACH(questions, char *, cp, tor_free(cp));
  1028. smartlist_free(answers);
  1029. smartlist_free(questions);
  1030. smartlist_free(unrecognized);
  1031. tor_free(msg);
  1032. return 0;
  1033. }
  1034. /** DOCDOC */
  1035. static int
  1036. handle_control_extendcircuit(connection_t *conn, uint32_t len,
  1037. const char *body)
  1038. {
  1039. /* XXXX V1 */
  1040. smartlist_t *router_nicknames, *routers;
  1041. uint32_t circ_id;
  1042. circuit_t *circ;
  1043. char reply[4];
  1044. if (len<5) {
  1045. send_control0_error(conn, ERR_SYNTAX, "extendcircuit message too short");
  1046. return 0;
  1047. }
  1048. router_nicknames = smartlist_create();
  1049. routers = smartlist_create();
  1050. smartlist_split_string(router_nicknames, body+4, ",", 0, 0);
  1051. SMARTLIST_FOREACH(router_nicknames, const char *, n,
  1052. {
  1053. routerinfo_t *r = router_get_by_nickname(n);
  1054. if (!r) {
  1055. send_control0_error(conn, ERR_NO_ROUTER, n);
  1056. goto done;
  1057. }
  1058. smartlist_add(routers, r);
  1059. });
  1060. if (!smartlist_len(routers)) {
  1061. send_control0_error(conn, ERR_SYNTAX, "No router names provided");
  1062. goto done;
  1063. }
  1064. circ_id = ntohl(get_uint32(body));
  1065. if (!circ_id) {
  1066. /* start a new circuit */
  1067. circ = circuit_init(CIRCUIT_PURPOSE_C_GENERAL, 0, 0, 0);
  1068. } else {
  1069. circ = circuit_get_by_global_id(circ_id);
  1070. if (!circ) {
  1071. send_control0_error(conn, ERR_NO_CIRC,
  1072. "No circuit found with given ID");
  1073. goto done;
  1074. }
  1075. }
  1076. /* now circ refers to something that is ready to be extended */
  1077. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1078. {
  1079. circuit_append_new_exit(circ, r);
  1080. });
  1081. /* now that we've populated the cpath, start extending */
  1082. if (!circ_id) {
  1083. if (circuit_handle_first_hop(circ) < 0) {
  1084. circuit_mark_for_close(circ);
  1085. send_control0_error(conn, ERR_INTERNAL, "couldn't start circuit");
  1086. goto done;
  1087. }
  1088. } else {
  1089. if (circ->state == CIRCUIT_STATE_OPEN) {
  1090. circ->state = CIRCUIT_STATE_BUILDING;
  1091. if (circuit_send_next_onion_skin(circ) < 0) {
  1092. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  1093. circuit_mark_for_close(circ);
  1094. send_control0_error(conn, ERR_INTERNAL, "couldn't send onion skin");
  1095. goto done;
  1096. }
  1097. }
  1098. }
  1099. set_uint32(reply, htonl(circ->global_identifier));
  1100. send_control_done2(conn, reply, sizeof(reply));
  1101. done:
  1102. SMARTLIST_FOREACH(router_nicknames, char *, n, tor_free(n));
  1103. smartlist_free(router_nicknames);
  1104. smartlist_free(routers);
  1105. return 0;
  1106. }
  1107. /** DOCDOC */
  1108. static int
  1109. handle_control_attachstream(connection_t *conn, uint32_t len,
  1110. const char *body)
  1111. {
  1112. /* XXXX V1 */
  1113. uint32_t conn_id;
  1114. uint32_t circ_id;
  1115. connection_t *ap_conn;
  1116. circuit_t *circ;
  1117. if (len < 8) {
  1118. send_control0_error(conn, ERR_SYNTAX, "attachstream message too short");
  1119. return 0;
  1120. }
  1121. conn_id = ntohl(get_uint32(body));
  1122. circ_id = ntohl(get_uint32(body+4));
  1123. if (!(ap_conn = connection_get_by_global_id(conn_id))) {
  1124. send_control0_error(conn, ERR_NO_STREAM,
  1125. "No connection found with given ID");
  1126. return 0;
  1127. }
  1128. if (ap_conn->state != AP_CONN_STATE_CONTROLLER_WAIT) {
  1129. send_control0_error(conn, ERR_NO_STREAM,
  1130. "Connection was not managed by controller.");
  1131. return 0;
  1132. }
  1133. if (!circ_id) {
  1134. ap_conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  1135. if (connection_ap_handshake_attach_circuit(ap_conn)<0)
  1136. connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_CANT_ATTACH);
  1137. send_control_done(conn);
  1138. return 0;
  1139. }
  1140. if (!(circ = circuit_get_by_global_id(circ_id))) {
  1141. send_control0_error(conn, ERR_NO_CIRC, "No circuit found with given ID");
  1142. return 0;
  1143. }
  1144. if (circ->state != CIRCUIT_STATE_OPEN) {
  1145. send_control0_error(conn, ERR_INTERNAL, "Refuse to attach stream to non-open circ.");
  1146. return 0;
  1147. }
  1148. if (connection_ap_handshake_attach_chosen_circuit(ap_conn, circ) != 1) {
  1149. send_control0_error(conn, ERR_INTERNAL, "Unable to attach stream.");
  1150. return 0;
  1151. }
  1152. send_control_done(conn);
  1153. return 0;
  1154. }
  1155. /** DOCDOC */
  1156. static int
  1157. handle_control_postdescriptor(connection_t *conn, uint32_t len,
  1158. const char *body)
  1159. {
  1160. char *desc;
  1161. int v0 = STATE_IS_V0(conn->state);
  1162. if (v0)
  1163. desc = (char*)body;
  1164. else {
  1165. const char *cp = memchr(body, '\n', len);
  1166. tor_assert(cp);
  1167. read_escaped_data(cp, len-(cp-body), 1, &desc);
  1168. }
  1169. const char *msg=NULL;
  1170. switch (router_load_single_router(desc, &msg)) {
  1171. case -1:
  1172. if (!msg) msg = "Could not parse descriptor";
  1173. if (v0)
  1174. send_control0_error(conn,ERR_SYNTAX,msg);
  1175. else
  1176. connection_printf_to_buf(conn, "554 %s\r\n", msg);
  1177. break;
  1178. case 0:
  1179. if (!msg) msg = "Descriptor not added";
  1180. if (v0)
  1181. send_control_done2(conn,msg,0);
  1182. else
  1183. connection_printf_to_buf(conn, "251 %s\r\n",msg);
  1184. break;
  1185. case 1:
  1186. send_control_done(conn);
  1187. break;
  1188. }
  1189. return 0;
  1190. }
  1191. /** DOCDOC */
  1192. static int
  1193. handle_control_redirectstream(connection_t *conn, uint32_t len,
  1194. const char *body)
  1195. {
  1196. connection_t *ap_conn = NULL;
  1197. uint32_t conn_id;
  1198. char *new_addr = NULL;
  1199. if (STATE_IS_V0(conn->state)) {
  1200. if (len < 6) {
  1201. send_control0_error(conn, ERR_SYNTAX, "redirectstream message too short");
  1202. return 0;
  1203. }
  1204. conn_id = ntohl(get_uint32(body));
  1205. if (!(ap_conn = connection_get_by_global_id(conn_id))
  1206. || ap_conn->state != CONN_TYPE_AP
  1207. || !ap_conn->socks_request) {
  1208. send_control0_error(conn, ERR_NO_STREAM,
  1209. "No AP connection found with given ID");
  1210. return 0;
  1211. }
  1212. new_addr = tor_strdup(body+4);
  1213. } else {
  1214. smartlist_t *args;
  1215. args = smartlist_create();
  1216. smartlist_split_string(args, body, " ",
  1217. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1218. if (smartlist_len(args)<2)
  1219. connection_printf_to_buf(conn,"512 Missing argument to REDIRECTSTREAM\r\n");
  1220. else if (!(ap_conn = get_stream(smartlist_get(args, 0)))
  1221. || !ap_conn->socks_request) {
  1222. connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
  1223. (char*)smartlist_get(args, 0));
  1224. } else {
  1225. new_addr = tor_strdup(smartlist_get(args, 1));
  1226. }
  1227. SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
  1228. smartlist_free(args);
  1229. if (!new_addr)
  1230. return 0;
  1231. }
  1232. strlcpy(ap_conn->socks_request->address, new_addr,
  1233. sizeof(ap_conn->socks_request->address));
  1234. tor_free(new_addr);
  1235. send_control_done(conn);
  1236. return 0;
  1237. }
  1238. /** DOCDOC */
  1239. static int
  1240. handle_control_closestream(connection_t *conn, uint32_t len,
  1241. const char *body)
  1242. {
  1243. connection_t *ap_conn=NULL;
  1244. uint8_t reason=0;
  1245. if (STATE_IS_V0(conn->state)) {
  1246. uint32_t conn_id;
  1247. if (len < 6) {
  1248. send_control0_error(conn, ERR_SYNTAX, "closestream message too short");
  1249. return 0;
  1250. }
  1251. conn_id = ntohl(get_uint32(body));
  1252. reason = *(uint8_t*)(body+4);
  1253. if (!(ap_conn = connection_get_by_global_id(conn_id))
  1254. || ap_conn->state != CONN_TYPE_AP
  1255. || !ap_conn->socks_request) {
  1256. send_control0_error(conn, ERR_NO_STREAM,
  1257. "No AP connection found with given ID");
  1258. return 0;
  1259. }
  1260. } else {
  1261. smartlist_t *args;
  1262. int ok;
  1263. args = smartlist_create();
  1264. smartlist_split_string(args, body, " ",
  1265. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1266. if (smartlist_len(args)<2)
  1267. connection_printf_to_buf(conn, "512 Missing argument to CLOSECIRCUIT\r\n");
  1268. else if (!(ap_conn = get_stream(smartlist_get(args, 0))))
  1269. connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
  1270. (char*)smartlist_get(args, 0));
  1271. else {
  1272. reason = (uint8_t) tor_parse_ulong(smartlist_get(args,1), 10, 0, 255,
  1273. &ok, NULL);
  1274. if (!ok) {
  1275. connection_printf_to_buf(conn, "552 Unrecognized reason \"%s\"\r\n",
  1276. (char*)smartlist_get(args, 1));
  1277. ap_conn = NULL;
  1278. }
  1279. }
  1280. SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
  1281. smartlist_free(args);
  1282. if (!ap_conn)
  1283. return 0;
  1284. }
  1285. connection_mark_unattached_ap(ap_conn, reason);
  1286. send_control_done(conn);
  1287. return 0;
  1288. }
  1289. /** DOCDOC */
  1290. static int
  1291. handle_control_closecircuit(connection_t *conn, uint32_t len,
  1292. const char *body)
  1293. {
  1294. circuit_t *circ = NULL;
  1295. int safe = 0;
  1296. if (STATE_IS_V0(conn->state)) {
  1297. uint32_t circ_id;
  1298. if (len < 5) {
  1299. send_control0_error(conn, ERR_SYNTAX, "closecircuit message too short");
  1300. return 0;
  1301. }
  1302. circ_id = ntohl(get_uint32(body));
  1303. safe = (*(uint8_t*)(body+4)) & 1;
  1304. if (!(circ = circuit_get_by_global_id(circ_id))) {
  1305. send_control0_error(conn, ERR_NO_CIRC,
  1306. "No circuit found with given ID");
  1307. return 0;
  1308. }
  1309. } else {
  1310. smartlist_t *args;
  1311. args = smartlist_create();
  1312. smartlist_split_string(args, body, " ",
  1313. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1314. if (smartlist_len(args)<1)
  1315. connection_printf_to_buf(conn, "512 Missing argument to CLOSECIRCUIT\r\n");
  1316. else if (!(circ=get_circ(smartlist_get(args, 0))))
  1317. connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
  1318. (char*)smartlist_get(args, 0));
  1319. else {
  1320. int i;
  1321. for (i=1; i < smartlist_len(args); ++i) {
  1322. if (!strcasecmp(smartlist_get(args, i), "IfUnused"))
  1323. safe = 1;
  1324. else
  1325. log_fn(LOG_INFO, "Skipping unknown option %s",
  1326. (char*)smartlist_get(args,i));
  1327. }
  1328. }
  1329. SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
  1330. smartlist_free(args);
  1331. if (!circ)
  1332. return 0;
  1333. }
  1334. if (!safe || !circ->p_streams) {
  1335. circuit_mark_for_close(circ);
  1336. }
  1337. send_control_done(conn);
  1338. return 0;
  1339. }
  1340. /** DOCDOC */
  1341. static int
  1342. handle_control_fragments(connection_t *conn, uint16_t command_type,
  1343. uint32_t body_len, char *body)
  1344. {
  1345. if (command_type == CONTROL0_CMD_FRAGMENTHEADER) {
  1346. if (conn->incoming_cmd) {
  1347. log_fn(LOG_WARN, "Dropping incomplete fragmented command");
  1348. tor_free(conn->incoming_cmd);
  1349. }
  1350. if (body_len < 6) {
  1351. send_control0_error(conn, ERR_SYNTAX, "FRAGMENTHEADER too short.");
  1352. return 0;
  1353. }
  1354. conn->incoming_cmd_type = ntohs(get_uint16(body));
  1355. conn->incoming_cmd_len = ntohl(get_uint32(body+2));
  1356. conn->incoming_cmd_cur_len = 0;
  1357. conn->incoming_cmd = tor_malloc(conn->incoming_cmd_len);
  1358. body += 6;
  1359. body_len -= 6;
  1360. } else if (command_type == CONTROL0_CMD_FRAGMENT) {
  1361. if (!conn->incoming_cmd) {
  1362. send_control0_error(conn, ERR_SYNTAX, "Out-of-place FRAGMENT");
  1363. return 0;
  1364. }
  1365. } else {
  1366. tor_assert(0);
  1367. }
  1368. if (conn->incoming_cmd_cur_len + body_len > conn->incoming_cmd_len) {
  1369. tor_free(conn->incoming_cmd);
  1370. send_control0_error(conn, ERR_SYNTAX,
  1371. "Fragmented data exceeds declared length");
  1372. return 0;
  1373. }
  1374. memcpy(conn->incoming_cmd + conn->incoming_cmd_cur_len,
  1375. body, body_len);
  1376. conn->incoming_cmd_cur_len += body_len;
  1377. return 0;
  1378. }
  1379. /** Called when <b>conn</b> has no more bytes left on its outbuf. */
  1380. int
  1381. connection_control_finished_flushing(connection_t *conn)
  1382. {
  1383. tor_assert(conn);
  1384. tor_assert(conn->type == CONN_TYPE_CONTROL);
  1385. connection_stop_writing(conn);
  1386. return 0;
  1387. }
  1388. /** Called when <b>conn</b> has gotten its socket closed. */
  1389. int
  1390. connection_control_reached_eof(connection_t *conn)
  1391. {
  1392. tor_assert(conn);
  1393. tor_assert(conn->type == CONN_TYPE_CONTROL);
  1394. log_fn(LOG_INFO,"Control connection reached EOF. Closing.");
  1395. connection_mark_for_close(conn);
  1396. return 0;
  1397. }
  1398. static int
  1399. connection_control_process_inbuf_v1(connection_t *conn)
  1400. {
  1401. size_t data_len, cmd_len;
  1402. char *args;
  1403. tor_assert(conn);
  1404. tor_assert(conn->type == CONN_TYPE_CONTROL);
  1405. tor_assert(conn->state == CONTROL_CONN_STATE_OPEN_V1 ||
  1406. conn->state == CONTROL_CONN_STATE_NEEDAUTH_V1);
  1407. if (!conn->incoming_cmd) {
  1408. conn->incoming_cmd = tor_malloc(1024);
  1409. conn->incoming_cmd_len = 1024;
  1410. conn->incoming_cmd_cur_len = 0;
  1411. }
  1412. again:
  1413. while (1) {
  1414. size_t last_idx;
  1415. int r;
  1416. do {
  1417. data_len = conn->incoming_cmd_len - conn->incoming_cmd_cur_len;
  1418. r = fetch_from_buf_line(conn->inbuf,
  1419. conn->incoming_cmd+conn->incoming_cmd_cur_len,
  1420. &data_len);
  1421. if (r == 0)
  1422. /* Line not all here yet. Wait. */
  1423. return 0;
  1424. else if (r == -1) {
  1425. while (conn->incoming_cmd_len < data_len+conn->incoming_cmd_cur_len)
  1426. conn->incoming_cmd_len *= 2;
  1427. conn->incoming_cmd = tor_realloc(conn->incoming_cmd, conn->incoming_cmd_len);
  1428. }
  1429. } while (r != 1);
  1430. tor_assert(data_len);
  1431. last_idx = conn->incoming_cmd_cur_len;
  1432. conn->incoming_cmd_cur_len += data_len;
  1433. /* We have appended a line to incoming_cmd. Is the command done? */
  1434. if (last_idx == 0 && *conn->incoming_cmd != '+')
  1435. /* One line command, didn't start with '+'. */
  1436. break;
  1437. if (last_idx+3 == conn->incoming_cmd_cur_len &&
  1438. !memcmp(conn->incoming_cmd + last_idx, ".\r\n", 3))
  1439. /* Just appended ".\r\n"; we're done. */
  1440. break;
  1441. /* Otherwise, read another line. */
  1442. }
  1443. data_len = conn->incoming_cmd_cur_len;
  1444. /* Okay, we now have a command sitting on conn->incoming_cmd. See if we
  1445. * recognize it.
  1446. */
  1447. cmd_len = 0;
  1448. while (cmd_len < data_len && !isspace(conn->incoming_cmd[cmd_len]))
  1449. ++cmd_len;
  1450. /*
  1451. log_fn(LOG_NOTICE, "READ A COMMAND FROM THE BUFFER: <%s> [%d]",
  1452. conn->incoming_cmd, (int)cmd_len);
  1453. */
  1454. data_len -= cmd_len;
  1455. conn->incoming_cmd[cmd_len]='\0';
  1456. args = conn->incoming_cmd+cmd_len+1;
  1457. while (*args == ' ' || *args == '\t') {
  1458. ++args;
  1459. --data_len;
  1460. }
  1461. /*
  1462. log_fn(LOG_NOTICE, "COMMAND IS: <%s>", conn->incoming_cmd);
  1463. log_fn(LOG_NOTICE, "ARGS ARE: <%s>", args);
  1464. */
  1465. if (conn->state == CONTROL_CONN_STATE_NEEDAUTH_V1 &&
  1466. strcasecmp(conn->incoming_cmd, "AUTHENTICATE")) {
  1467. connection_write_str_to_buf("514 Authentication required.\r\n", conn);
  1468. conn->incoming_cmd_cur_len = 0;
  1469. goto again;
  1470. }
  1471. if (!strcasecmp(conn->incoming_cmd, "SETCONF")) {
  1472. if (handle_control_setconf(conn, data_len, args))
  1473. return -1;
  1474. } else if (!strcasecmp(conn->incoming_cmd, "GETCONF")) {
  1475. if (handle_control_getconf(conn, data_len, args))
  1476. return -1;
  1477. } else if (!strcasecmp(conn->incoming_cmd, "SETEVENTS")) {
  1478. if (handle_control_setevents(conn, data_len, args))
  1479. return -1;
  1480. } else if (!strcasecmp(conn->incoming_cmd, "AUTHENTICATE")) {
  1481. if (handle_control_authenticate(conn, data_len, args))
  1482. return -1;
  1483. } else if (!strcasecmp(conn->incoming_cmd, "SAVECONF")) {
  1484. if (handle_control_saveconf(conn, data_len, args))
  1485. return -1;
  1486. } else if (!strcasecmp(conn->incoming_cmd, "SIGNAL")) {
  1487. if (handle_control_signal(conn, data_len, args))
  1488. return -1;
  1489. } else if (!strcasecmp(conn->incoming_cmd, "MAPADDRESS")) {
  1490. if (handle_control_mapaddress(conn, data_len, args))
  1491. return -1;
  1492. } else if (!strcasecmp(conn->incoming_cmd, "GETINFO")) {
  1493. if (handle_control_getinfo(conn, data_len, args))
  1494. return -1;
  1495. } else if (!strcasecmp(conn->incoming_cmd, "EXTENDCIRCUIT")) {
  1496. if (handle_control_extendcircuit(conn, data_len, args))
  1497. return -1;
  1498. } else if (!strcasecmp(conn->incoming_cmd, "ATTACHSTREAM")) {
  1499. if (handle_control_attachstream(conn, data_len, args))
  1500. return -1;
  1501. } else if (!strcasecmp(conn->incoming_cmd, "+POSTDESCRIPTOR")) {
  1502. if (handle_control_postdescriptor(conn, data_len, args))
  1503. return -1;
  1504. } else if (!strcasecmp(conn->incoming_cmd, "REDIRECTSTREAM")) {
  1505. if (handle_control_redirectstream(conn, data_len, args))
  1506. return -1;
  1507. } else if (!strcasecmp(conn->incoming_cmd, "CLOSESTREAM")) {
  1508. if (handle_control_closestream(conn, data_len, args))
  1509. return -1;
  1510. } else if (!strcasecmp(conn->incoming_cmd, "CLOSECIRCUIT")) {
  1511. if (handle_control_closecircuit(conn, data_len, args))
  1512. return -1;
  1513. } else {
  1514. connection_printf_to_buf(conn, "510 Unrecognized command \"%s\"\r\n",
  1515. conn->incoming_cmd);
  1516. }
  1517. conn->incoming_cmd_cur_len = 0;
  1518. goto again;
  1519. }
  1520. static int
  1521. connection_control_process_inbuf_v0(connection_t *conn)
  1522. {
  1523. uint32_t body_len;
  1524. uint16_t command_type;
  1525. char *body;
  1526. again:
  1527. /* Try to suck a control message from the buffer. */
  1528. switch (fetch_from_buf_control0(conn->inbuf, &body_len, &command_type, &body,
  1529. conn->state == CONTROL_CONN_STATE_NEEDAUTH_V0))
  1530. {
  1531. case -2:
  1532. tor_free(body);
  1533. log_fn(LOG_INFO, "Detected v1 contol protocol on connection (fd %d)",
  1534. conn->s);
  1535. conn->state = CONTROL_CONN_STATE_NEEDAUTH_V1;
  1536. return connection_control_process_inbuf_v1(conn);
  1537. case -1:
  1538. tor_free(body);
  1539. log_fn(LOG_WARN, "Error in control command. Failing.");
  1540. return -1;
  1541. case 0:
  1542. /* Control command not all here yet. Wait. */
  1543. return 0;
  1544. case 1:
  1545. /* We got a command. Process it. */
  1546. break;
  1547. default:
  1548. tor_assert(0);
  1549. }
  1550. /* We got a command. If we need authentication, only authentication
  1551. * commands will be considered. */
  1552. if (conn->state == CONTROL_CONN_STATE_NEEDAUTH_V0 &&
  1553. command_type != CONTROL0_CMD_AUTHENTICATE) {
  1554. log_fn(LOG_WARN, "Rejecting '%s' command; authentication needed.",
  1555. control_cmd_to_string(command_type));
  1556. send_control0_error(conn, ERR_UNAUTHORIZED, "Authentication required");
  1557. tor_free(body);
  1558. goto again;
  1559. }
  1560. if (command_type == CONTROL0_CMD_FRAGMENTHEADER ||
  1561. command_type == CONTROL0_CMD_FRAGMENT) {
  1562. if (handle_control_fragments(conn, command_type, body_len, body))
  1563. return -1;
  1564. tor_free(body);
  1565. if (conn->incoming_cmd_cur_len != conn->incoming_cmd_len)
  1566. goto again;
  1567. command_type = conn->incoming_cmd_type;
  1568. body_len = conn->incoming_cmd_len;
  1569. body = conn->incoming_cmd;
  1570. conn->incoming_cmd = NULL;
  1571. } else if (conn->incoming_cmd) {
  1572. log_fn(LOG_WARN, "Dropping incomplete fragmented command");
  1573. tor_free(conn->incoming_cmd);
  1574. }
  1575. /* Okay, we're willing to process the command. */
  1576. switch (command_type)
  1577. {
  1578. case CONTROL0_CMD_SETCONF:
  1579. if (handle_control_setconf(conn, body_len, body))
  1580. return -1;
  1581. break;
  1582. case CONTROL0_CMD_GETCONF:
  1583. if (handle_control_getconf(conn, body_len, body))
  1584. return -1;
  1585. break;
  1586. case CONTROL0_CMD_SETEVENTS:
  1587. if (handle_control_setevents(conn, body_len, body))
  1588. return -1;
  1589. break;
  1590. case CONTROL0_CMD_AUTHENTICATE:
  1591. if (handle_control_authenticate(conn, body_len, body))
  1592. return -1;
  1593. break;
  1594. case CONTROL0_CMD_SAVECONF:
  1595. if (handle_control_saveconf(conn, body_len, body))
  1596. return -1;
  1597. break;
  1598. case CONTROL0_CMD_SIGNAL:
  1599. if (handle_control_signal(conn, body_len, body))
  1600. return -1;
  1601. break;
  1602. case CONTROL0_CMD_MAPADDRESS:
  1603. if (handle_control_mapaddress(conn, body_len, body))
  1604. return -1;
  1605. break;
  1606. case CONTROL0_CMD_GETINFO:
  1607. if (handle_control_getinfo(conn, body_len, body))
  1608. return -1;
  1609. break;
  1610. case CONTROL0_CMD_EXTENDCIRCUIT:
  1611. if (handle_control_extendcircuit(conn, body_len, body))
  1612. return -1;
  1613. break;
  1614. case CONTROL0_CMD_ATTACHSTREAM:
  1615. if (handle_control_attachstream(conn, body_len, body))
  1616. return -1;
  1617. break;
  1618. case CONTROL0_CMD_POSTDESCRIPTOR:
  1619. if (handle_control_postdescriptor(conn, body_len, body))
  1620. return -1;
  1621. break;
  1622. case CONTROL0_CMD_REDIRECTSTREAM:
  1623. if (handle_control_redirectstream(conn, body_len, body))
  1624. return -1;
  1625. break;
  1626. case CONTROL0_CMD_CLOSESTREAM:
  1627. if (handle_control_closestream(conn, body_len, body))
  1628. return -1;
  1629. break;
  1630. case CONTROL0_CMD_CLOSECIRCUIT:
  1631. if (handle_control_closecircuit(conn, body_len, body))
  1632. return -1;
  1633. break;
  1634. case CONTROL0_CMD_ERROR:
  1635. case CONTROL0_CMD_DONE:
  1636. case CONTROL0_CMD_CONFVALUE:
  1637. case CONTROL0_CMD_EVENT:
  1638. case CONTROL0_CMD_INFOVALUE:
  1639. log_fn(LOG_WARN, "Received client-only '%s' command; ignoring.",
  1640. control_cmd_to_string(command_type));
  1641. send_control0_error(conn, ERR_UNRECOGNIZED_TYPE,
  1642. "Command type only valid from server to tor client");
  1643. break;
  1644. case CONTROL0_CMD_FRAGMENTHEADER:
  1645. case CONTROL0_CMD_FRAGMENT:
  1646. log_fn(LOG_WARN, "Recieved command fragment out of order; ignoring.");
  1647. send_control0_error(conn, ERR_SYNTAX, "Bad fragmentation on command.");
  1648. default:
  1649. log_fn(LOG_WARN, "Received unrecognized command type %d; ignoring.",
  1650. (int)command_type);
  1651. send_control0_error(conn, ERR_UNRECOGNIZED_TYPE,
  1652. "Unrecognized command type");
  1653. break;
  1654. }
  1655. tor_free(body);
  1656. goto again; /* There might be more data. */
  1657. }
  1658. /** Called when <b>conn</b> has received more bytes on its inbuf.
  1659. */
  1660. int
  1661. connection_control_process_inbuf(connection_t *conn)
  1662. {
  1663. tor_assert(conn);
  1664. tor_assert(conn->type == CONN_TYPE_CONTROL);
  1665. if (STATE_IS_V0(conn->state))
  1666. return connection_control_process_inbuf_v0(conn);
  1667. else
  1668. return connection_control_process_inbuf_v1(conn);
  1669. }
  1670. /** Something has happened to circuit <b>circ</b>: tell any interested
  1671. * control connections. */
  1672. int
  1673. control_event_circuit_status(circuit_t *circ, circuit_status_event_t tp)
  1674. {
  1675. char *path, *msg;
  1676. if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
  1677. return 0;
  1678. tor_assert(circ);
  1679. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  1680. path = circuit_list_path(circ,0);
  1681. if (EVENT_IS_INTERESTING0(EVENT_CIRCUIT_STATUS)) {
  1682. size_t path_len = strlen(path);
  1683. msg = tor_malloc(1+4+path_len+1); /* event, circid, path, NUL. */
  1684. msg[0] = (uint8_t) tp;
  1685. set_uint32(msg+1, htonl(circ->global_identifier));
  1686. strlcpy(msg+5,path,path_len+1);
  1687. send_control0_event(EVENT_CIRCUIT_STATUS, (uint32_t)(path_len+6), msg);
  1688. tor_free(msg);
  1689. }
  1690. if (EVENT_IS_INTERESTING1(EVENT_CIRCUIT_STATUS)) {
  1691. const char *status;
  1692. switch (tp)
  1693. {
  1694. case CIRC_EVENT_LAUNCHED: status = "LAUNCHED"; break;
  1695. case CIRC_EVENT_BUILT: status = "BUILT"; break;
  1696. case CIRC_EVENT_EXTENDED: status = "EXTENDED"; break;
  1697. case CIRC_EVENT_FAILED: status = "FAILED"; break;
  1698. case CIRC_EVENT_CLOSED: status = "CLOSED"; break;
  1699. default:
  1700. log_fn(LOG_WARN, "Unrecognized status code %d", (int)tp);
  1701. return 0;
  1702. }
  1703. send_control1_event(EVENT_CIRCUIT_STATUS,
  1704. "650 CIRC %lu %s %s\r\n",
  1705. (unsigned long)circ->global_identifier,
  1706. status, path);
  1707. }
  1708. tor_free(path);
  1709. return 0;
  1710. }
  1711. /** Something has happened to the stream associated with AP connection
  1712. * <b>conn</b>: tell any interested control connections. */
  1713. int
  1714. control_event_stream_status(connection_t *conn, stream_status_event_t tp)
  1715. {
  1716. char *msg;
  1717. size_t len;
  1718. char buf[256], buf2[256];
  1719. tor_assert(conn->type == CONN_TYPE_AP);
  1720. tor_assert(conn->socks_request);
  1721. if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
  1722. return 0;
  1723. if (conn->chosen_exit_name)
  1724. tor_snprintf(buf2, sizeof(buf2), ".%s.exit", conn->chosen_exit_name);
  1725. tor_snprintf(buf, sizeof(buf), "%s%s:%d",
  1726. conn->socks_request->address,
  1727. conn->chosen_exit_name ? buf2 : "",
  1728. conn->socks_request->port);
  1729. if (EVENT_IS_INTERESTING0(EVENT_STREAM_STATUS)) {
  1730. len = strlen(buf);
  1731. msg = tor_malloc(5+len+1);
  1732. msg[0] = (uint8_t) tp;
  1733. set_uint32(msg+1, htonl(conn->global_identifier));
  1734. strlcpy(msg+5, buf, len+1);
  1735. send_control0_event(EVENT_STREAM_STATUS, (uint32_t)(5+len+1), msg);
  1736. tor_free(msg);
  1737. }
  1738. if (EVENT_IS_INTERESTING0(EVENT_STREAM_STATUS)) {
  1739. const char *status;
  1740. switch (tp)
  1741. {
  1742. case STREAM_EVENT_SENT_CONNECT: status = "SENTCONNECT"; break;
  1743. case STREAM_EVENT_SENT_RESOLVE: status = "SENTRESOLVE"; break;
  1744. case STREAM_EVENT_SUCCEEDED: status = "SUCCEEDED"; break;
  1745. case STREAM_EVENT_FAILED: status = "FAILED"; break;
  1746. case STREAM_EVENT_CLOSED: status = "CLOSED"; break;
  1747. case STREAM_EVENT_NEW: status = "NEW"; break;
  1748. case STREAM_EVENT_NEW_RESOLVE: status = "NEWRESOLVE"; break;
  1749. case STREAM_EVENT_FAILED_RETRIABLE: status = "DETACHED"; break;
  1750. default:
  1751. log_fn(LOG_WARN, "Unrecognized status code %d", (int)tp);
  1752. return 0;
  1753. }
  1754. send_control1_event(EVENT_STREAM_STATUS,
  1755. "650 STREAM %lu %s %s\r\n",
  1756. (unsigned long)conn->global_identifier,
  1757. status, buf);
  1758. }
  1759. return 0;
  1760. }
  1761. /** Something has happened to the OR connection <b>conn</b>: tell any
  1762. * interested control connections. */
  1763. int
  1764. control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp)
  1765. {
  1766. char buf[HEX_DIGEST_LEN+3]; /* status, dollar, identity, NUL */
  1767. size_t len;
  1768. tor_assert(conn->type == CONN_TYPE_OR);
  1769. if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
  1770. return 0;
  1771. if (EVENT_IS_INTERESTING0(EVENT_OR_CONN_STATUS)) {
  1772. buf[0] = (uint8_t)tp;
  1773. strlcpy(buf+1,conn->nickname,sizeof(buf)-1);
  1774. len = strlen(buf+1);
  1775. send_control0_event(EVENT_OR_CONN_STATUS, (uint32_t)(len+1), buf);
  1776. }
  1777. if (EVENT_IS_INTERESTING1(EVENT_OR_CONN_STATUS)) {
  1778. const char *status;
  1779. switch (tp)
  1780. {
  1781. case OR_CONN_EVENT_LAUNCHED: status = "LAUNCHED"; break;
  1782. case OR_CONN_EVENT_CONNECTED: status = "CONNECTED"; break;
  1783. case OR_CONN_EVENT_FAILED: status = "FAILED"; break;
  1784. case OR_CONN_EVENT_CLOSED: status = "CLOSED"; break;
  1785. default:
  1786. log_fn(LOG_WARN, "Unrecognized status code %d", (int)tp);
  1787. return 0;
  1788. }
  1789. send_control1_event(EVENT_OR_CONN_STATUS,
  1790. "650 ORCONN %s %s\r\n",
  1791. conn->nickname, status);
  1792. }
  1793. return 0;
  1794. }
  1795. /** A second or more has elapsed: tell any interested control
  1796. * connections how much bandwidth we used. */
  1797. int
  1798. control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
  1799. {
  1800. char buf[8];
  1801. if (EVENT_IS_INTERESTING0(EVENT_BANDWIDTH_USED)) {
  1802. set_uint32(buf, htonl(n_read));
  1803. set_uint32(buf+4, htonl(n_written));
  1804. send_control0_event(EVENT_BANDWIDTH_USED, 8, buf);
  1805. }
  1806. if (EVENT_IS_INTERESTING1(EVENT_BANDWIDTH_USED)) {
  1807. send_control1_event(EVENT_BANDWIDTH_USED,
  1808. "650 BW %lu %lu\r\n",
  1809. (unsigned long)n_read,
  1810. (unsigned long)n_written);
  1811. }
  1812. return 0;
  1813. }
  1814. /** We got a log message: tell any interested control connections. */
  1815. void
  1816. control_event_logmsg(int severity, const char *msg)
  1817. {
  1818. static int sending_logmsg=0;
  1819. int oldlog, event;
  1820. if (sending_logmsg)
  1821. return;
  1822. oldlog = EVENT_IS_INTERESTING0(EVENT_LOG_OBSOLETE) &&
  1823. (severity == LOG_NOTICE || severity == LOG_WARN || severity == LOG_ERR);
  1824. event = log_severity_to_event(severity);
  1825. if (event<0 || !EVENT_IS_INTERESTING0(event))
  1826. event = 0;
  1827. if (oldlog || event) {
  1828. size_t len = strlen(msg);
  1829. sending_logmsg = 1;
  1830. if (event)
  1831. send_control0_event(event, (uint32_t)(len+1), msg);
  1832. if (oldlog)
  1833. send_control0_event(EVENT_LOG_OBSOLETE, (uint32_t)(len+1), msg);
  1834. sending_logmsg = 0;
  1835. }
  1836. event = log_severity_to_event(severity);
  1837. if (event >= 0 && EVENT_IS_INTERESTING1(event)) {
  1838. char *b = NULL;
  1839. const char *s;
  1840. if (strchr(msg, '\n')) {
  1841. char *cp;
  1842. b = tor_strdup(msg);
  1843. for (cp = b; *cp; ++cp)
  1844. if (*cp == '\r' || *cp == '\n')
  1845. *cp = ' ';
  1846. }
  1847. switch(severity) {
  1848. case LOG_INFO: s = "INFO"; break;
  1849. case LOG_NOTICE: s = "NOTICE"; break;
  1850. case LOG_WARN: s = "WARN"; break;
  1851. case LOG_ERR: s = "ERR"; break;
  1852. default: s = "UnknownLogSeverity"; break;
  1853. }
  1854. send_control1_event(event, "650 %s %s\r\n", s, b?b:msg);
  1855. tor_free(b);
  1856. }
  1857. }
  1858. /** Called whenever we receive new router descriptors: tell any
  1859. * interested control connections. <b>routers</b> is a list of
  1860. * DIGEST_LEN-byte identity digests.
  1861. */
  1862. int
  1863. control_event_descriptors_changed(smartlist_t *routers)
  1864. {
  1865. size_t len;
  1866. char *msg;
  1867. smartlist_t *identities;
  1868. char buf[HEX_DIGEST_LEN+1];
  1869. if (!EVENT_IS_INTERESTING(EVENT_NEW_DESC))
  1870. return 0;
  1871. identities = smartlist_create();
  1872. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1873. {
  1874. base16_encode(buf,sizeof(buf),r->identity_digest,DIGEST_LEN);
  1875. smartlist_add(identities, tor_strdup(buf));
  1876. });
  1877. if (EVENT_IS_INTERESTING0(EVENT_NEW_DESC)) {
  1878. msg = smartlist_join_strings(identities, ",", 0, &len);
  1879. send_control0_event(EVENT_NEW_DESC, len+1, msg);
  1880. tor_free(msg);
  1881. }
  1882. if (EVENT_IS_INTERESTING1(EVENT_NEW_DESC)) {
  1883. msg = smartlist_join_strings(identities, " ", 0, &len);
  1884. send_control1_event(EVENT_NEW_DESC, "650 NEWDESC %s\r\n", msg);
  1885. tor_free(msg);
  1886. }
  1887. SMARTLIST_FOREACH(identities, char *, cp, tor_free(cp));
  1888. smartlist_free(identities);
  1889. return 0;
  1890. }
  1891. /** Choose a random authentication cookie and write it to disk.
  1892. * Anybody who can read the cookie from disk will be considered
  1893. * authorized to use the control connection. */
  1894. int
  1895. init_cookie_authentication(int enabled)
  1896. {
  1897. char fname[512];
  1898. if (!enabled) {
  1899. authentication_cookie_is_set = 0;
  1900. return 0;
  1901. }
  1902. tor_snprintf(fname, sizeof(fname), "%s/control_auth_cookie",
  1903. get_options()->DataDirectory);
  1904. crypto_rand(authentication_cookie, AUTHENTICATION_COOKIE_LEN);
  1905. authentication_cookie_is_set = 1;
  1906. if (write_bytes_to_file(fname, authentication_cookie,
  1907. AUTHENTICATION_COOKIE_LEN, 1)) {
  1908. log_fn(LOG_WARN,"Error writing authentication cookie.");
  1909. return -1;
  1910. }
  1911. return 0;
  1912. }