control.c 73 KB

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