transports.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /* Copyright (c) 2011-2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file transports.c
  5. * \brief Pluggable Transports related code.
  6. *
  7. * \details
  8. * Each managed proxy is represented by a <b>managed_proxy_t</b>.
  9. * Each managed proxy can support multiple transports.
  10. * Each managed proxy gets configured through a multistep process.
  11. *
  12. * ::managed_proxy_list contains all the managed proxies this tor
  13. * instance is supporting.
  14. * In the ::managed_proxy_list there are ::unconfigured_proxies_n
  15. * managed proxies that are still unconfigured.
  16. *
  17. * In every run_scheduled_event() tick, we attempt to launch and then
  18. * configure the unconfiged managed proxies, using the configuration
  19. * protocol defined in the 180_pluggable_transport.txt proposal. A
  20. * managed proxy might need several ticks to get fully configured.
  21. *
  22. * When a managed proxy is fully configured, we register all its
  23. * transports to the circuitbuild.c subsystem. At that point the
  24. * transports are owned by the circuitbuild.c subsystem.
  25. *
  26. * When a managed proxy fails to follow the 180 configuration
  27. * protocol, it gets marked as broken and gets destroyed.
  28. *
  29. * <b>In a little more detail:</b>
  30. *
  31. * While we are serially parsing torrc, we store all the transports
  32. * that a proxy should spawn in its <em>transports_to_launch</em>
  33. * element.
  34. *
  35. * When we finish reading the torrc, we spawn the managed proxy and
  36. * expect {S,C}METHOD lines from its output. We add transports
  37. * described by METHOD lines to its <em>transports</em> element, as
  38. * transport_t structs.
  39. *
  40. * When the managed proxy stops spitting METHOD lines (signified by a
  41. * '{S,C}METHODS DONE' message) we register all the transports
  42. * collected to the circuitbuild.c subsystem. At this point, the
  43. * pointers to transport_t can be transformed into dangling pointers
  44. * at any point by the circuitbuild.c subsystem, and so we replace all
  45. * transport_t pointers with strings describing the transport names.
  46. * We can still go from a transport name to a transport_t using the
  47. * fact that each transport name uniquely identifies a transport_t.
  48. *
  49. * <b>In even more detail, this is what happens when a SIGHUP
  50. * occurs:</b>
  51. *
  52. * We immediately destroy all unconfigured proxies (We shouldn't have
  53. * unconfigured proxies in the first place, except when SIGHUP rings
  54. * immediately after tor is launched.).
  55. *
  56. * We mark all managed proxies and transports to signify that they
  57. * must be removed if they don't contribute by the new torrc
  58. * (we mark using the <b>marked_for_removal</b> element).
  59. * We also mark all managed proxies to signify that they might need to
  60. * be restarted so that they end up supporting all the transports the
  61. * new torrc wants them to support (using the <b>got_hup</b> element).
  62. * We also clear their <b>transports_to_launch</b> list so that we can
  63. * put there the transports we need to launch according to the new
  64. * torrc.
  65. *
  66. * We then start parsing torrc again.
  67. *
  68. * Everytime we encounter a transport line using a known pre-SIGHUP
  69. * managed proxy, we cleanse that proxy from the removal mark.
  70. * We also mark it as unconfigured so that on the next scheduled
  71. * events tick, we investigate whether we need to restart the proxy
  72. * so that it also spawns the new transports.
  73. * If the post-SIGHUP <b>transports_to_launch</b> list is identical to
  74. * the pre-SIGHUP one, it means that no changes were introduced to
  75. * this proxy during the SIGHUP and no restart has to take place.
  76. *
  77. * During the post-SIGHUP torrc parsing, we unmark all transports
  78. * spawned by managed proxies that we find in our torrc.
  79. * We do that so that if we don't need to restart a managed proxy, we
  80. * can continue using its old transports normally.
  81. * If we end up restarting the proxy, we destroy and unregister all
  82. * old transports from the circuitbuild.c subsystem.
  83. **/
  84. #define PT_PRIVATE
  85. #include "or.h"
  86. #include "config.h"
  87. #include "circuitbuild.h"
  88. #include "transports.h"
  89. #include "util.h"
  90. #include "router.h"
  91. static process_environment_t *
  92. create_managed_proxy_environment(const managed_proxy_t *mp);
  93. static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
  94. static void managed_proxy_destroy(managed_proxy_t *mp,
  95. int also_terminate_process);
  96. static void handle_finished_proxy(managed_proxy_t *mp);
  97. static void configure_proxy(managed_proxy_t *mp);
  98. static void parse_method_error(const char *line, int is_server_method);
  99. #define parse_server_method_error(l) parse_method_error(l, 1)
  100. #define parse_client_method_error(l) parse_method_error(l, 0)
  101. static INLINE void free_execve_args(char **arg);
  102. /** Managed proxy protocol strings */
  103. #define PROTO_ENV_ERROR "ENV-ERROR"
  104. #define PROTO_NEG_SUCCESS "VERSION"
  105. #define PROTO_NEG_FAIL "VERSION-ERROR no-version"
  106. #define PROTO_CMETHOD "CMETHOD"
  107. #define PROTO_SMETHOD "SMETHOD"
  108. #define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
  109. #define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
  110. #define PROTO_CMETHODS_DONE "CMETHODS DONE"
  111. #define PROTO_SMETHODS_DONE "SMETHODS DONE"
  112. /** Number of environment variables for managed proxy clients/servers. */
  113. #define ENVIRON_SIZE_CLIENT 3
  114. #define ENVIRON_SIZE_SERVER 7 /* XXX known to be too high, but that's ok */
  115. /** The first and only supported - at the moment - configuration
  116. protocol version. */
  117. #define PROTO_VERSION_ONE 1
  118. /** List of unconfigured managed proxies. */
  119. static smartlist_t *managed_proxy_list = NULL;
  120. /** Number of still unconfigured proxies. */
  121. static int unconfigured_proxies_n = 0;
  122. /** Boolean: True iff we might need to restart some proxies. */
  123. static int check_if_restarts_needed = 0;
  124. /** Return true if there are still unconfigured managed proxies, or proxies
  125. * that need restarting. */
  126. int
  127. pt_proxies_configuration_pending(void)
  128. {
  129. return unconfigured_proxies_n || check_if_restarts_needed;
  130. }
  131. /** Assert that the unconfigured_proxies_n value correctly matches the number
  132. * of proxies in a state other than PT_PROTO_COMPLETE. */
  133. static void
  134. assert_unconfigured_count_ok(void)
  135. {
  136. int n_completed = 0;
  137. if (!managed_proxy_list) {
  138. tor_assert(unconfigured_proxies_n == 0);
  139. return;
  140. }
  141. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
  142. if (mp->conf_state == PT_PROTO_COMPLETED)
  143. ++n_completed;
  144. });
  145. tor_assert(n_completed + unconfigured_proxies_n ==
  146. smartlist_len(managed_proxy_list));
  147. }
  148. /** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
  149. static int
  150. managed_proxy_has_argv(const managed_proxy_t *mp, char **proxy_argv)
  151. {
  152. char **tmp1=proxy_argv;
  153. char **tmp2=mp->argv;
  154. tor_assert(tmp1);
  155. tor_assert(tmp2);
  156. while (*tmp1 && *tmp2) {
  157. if (strcmp(*tmp1++, *tmp2++))
  158. return 0;
  159. }
  160. if (!*tmp1 && !*tmp2)
  161. return 1;
  162. return 0;
  163. }
  164. /** Return a managed proxy with the same argv as <b>proxy_argv</b>.
  165. * If no such managed proxy exists, return NULL. */
  166. static managed_proxy_t *
  167. get_managed_proxy_by_argv_and_type(char **proxy_argv, int is_server)
  168. {
  169. if (!managed_proxy_list)
  170. return NULL;
  171. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  172. if (managed_proxy_has_argv(mp, proxy_argv) &&
  173. mp->is_server == is_server)
  174. return mp;
  175. } SMARTLIST_FOREACH_END(mp);
  176. return NULL;
  177. }
  178. /** Add <b>transport</b> to managed proxy <b>mp</b>. */
  179. static void
  180. add_transport_to_proxy(const char *transport, managed_proxy_t *mp)
  181. {
  182. tor_assert(mp->transports_to_launch);
  183. if (!smartlist_string_isin(mp->transports_to_launch, transport))
  184. smartlist_add(mp->transports_to_launch, tor_strdup(transport));
  185. }
  186. /** Called when a SIGHUP occurs. Returns true if managed proxy
  187. * <b>mp</b> needs to be restarted after the SIGHUP, based on the new
  188. * torrc. */
  189. static int
  190. proxy_needs_restart(const managed_proxy_t *mp)
  191. {
  192. /* mp->transport_to_launch is populated with the names of the
  193. transports that must be launched *after* the SIGHUP.
  194. mp->transports is populated with the names of the transports that
  195. were launched *before* the SIGHUP.
  196. If the two lists contain the same strings, we don't need to
  197. restart the proxy, since it already does what we want. */
  198. tor_assert(smartlist_len(mp->transports_to_launch) > 0);
  199. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  200. if (smartlist_len(mp->transports_to_launch) != smartlist_len(mp->transports))
  201. goto needs_restart;
  202. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t_t_l) {
  203. if (!smartlist_string_isin(mp->transports, t_t_l))
  204. goto needs_restart;
  205. } SMARTLIST_FOREACH_END(t_t_l);
  206. return 0;
  207. needs_restart:
  208. return 1;
  209. }
  210. /** Managed proxy <b>mp</b> must be restarted. Do all the necessary
  211. * preparations and then flag its state so that it will be relaunched
  212. * in the next tick. */
  213. static void
  214. proxy_prepare_for_restart(managed_proxy_t *mp)
  215. {
  216. transport_t *t_tmp = NULL;
  217. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  218. /* destroy the process handle and terminate the process. */
  219. tor_process_handle_destroy(mp->process_handle, 1);
  220. mp->process_handle = NULL;
  221. /* destroy all its old transports. we no longer use them. */
  222. SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
  223. t_tmp = transport_get_by_name(t_name);
  224. if (t_tmp)
  225. t_tmp->marked_for_removal = 1;
  226. } SMARTLIST_FOREACH_END(t_name);
  227. sweep_transport_list();
  228. /* free the transport names in mp->transports */
  229. SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  230. smartlist_clear(mp->transports);
  231. /* flag it as an infant proxy so that it gets launched on next tick */
  232. mp->conf_state = PT_PROTO_INFANT;
  233. unconfigured_proxies_n++;
  234. }
  235. /** Launch managed proxy <b>mp</b>. */
  236. static int
  237. launch_managed_proxy(managed_proxy_t *mp)
  238. {
  239. int retval;
  240. process_environment_t *env = create_managed_proxy_environment(mp);
  241. #ifdef _WIN32
  242. /* Passing NULL as lpApplicationName makes Windows search for the .exe */
  243. retval = tor_spawn_background(NULL,
  244. (const char **)mp->argv,
  245. env,
  246. &mp->process_handle);
  247. #else
  248. retval = tor_spawn_background(mp->argv[0],
  249. (const char **)mp->argv,
  250. env,
  251. &mp->process_handle);
  252. #endif
  253. process_environment_free(env);
  254. if (retval == PROCESS_STATUS_ERROR) {
  255. log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
  256. mp->argv[0]);
  257. return -1;
  258. }
  259. log_info(LD_CONFIG, "Managed proxy at '%s' has spawned with PID '%d'.",
  260. mp->argv[0], tor_process_get_pid(mp->process_handle));
  261. mp->conf_state = PT_PROTO_LAUNCHED;
  262. return 0;
  263. }
  264. /** Check if any of the managed proxies we are currently trying to
  265. * configure have anything new to say. This is called from
  266. * run_scheduled_events(). */
  267. void
  268. pt_configure_remaining_proxies(void)
  269. {
  270. smartlist_t *tmp = smartlist_new();
  271. log_debug(LD_CONFIG, "Configuring remaining managed proxies (%d)!",
  272. unconfigured_proxies_n);
  273. /* Iterate over tmp, not managed_proxy_list, since configure_proxy can
  274. * remove elements from managed_proxy_list. */
  275. smartlist_add_all(tmp, managed_proxy_list);
  276. assert_unconfigured_count_ok();
  277. SMARTLIST_FOREACH_BEGIN(tmp, managed_proxy_t *, mp) {
  278. tor_assert(mp->conf_state != PT_PROTO_BROKEN ||
  279. mp->conf_state != PT_PROTO_FAILED_LAUNCH);
  280. if (mp->got_hup) {
  281. mp->got_hup = 0;
  282. /* This proxy is marked by a SIGHUP. Check whether we need to
  283. restart it. */
  284. if (proxy_needs_restart(mp)) {
  285. log_info(LD_GENERAL, "Preparing managed proxy '%s' for restart.",
  286. mp->argv[0]);
  287. proxy_prepare_for_restart(mp);
  288. } else { /* it doesn't need to be restarted. */
  289. log_info(LD_GENERAL, "Nothing changed for managed proxy '%s' after "
  290. "HUP: not restarting.", mp->argv[0]);
  291. }
  292. continue;
  293. }
  294. /* If the proxy is not fully configured, try to configure it
  295. futher. */
  296. if (!proxy_configuration_finished(mp))
  297. configure_proxy(mp);
  298. } SMARTLIST_FOREACH_END(mp);
  299. smartlist_free(tmp);
  300. check_if_restarts_needed = 0;
  301. assert_unconfigured_count_ok();
  302. }
  303. #ifdef _WIN32
  304. /** Attempt to continue configuring managed proxy <b>mp</b>. */
  305. static void
  306. configure_proxy(managed_proxy_t *mp)
  307. {
  308. int pos;
  309. char stdout_buf[200];
  310. smartlist_t *lines = NULL;
  311. /* if we haven't launched the proxy yet, do it now */
  312. if (mp->conf_state == PT_PROTO_INFANT) {
  313. if (launch_managed_proxy(mp) < 0) { /* launch fail */
  314. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  315. handle_finished_proxy(mp);
  316. }
  317. return;
  318. }
  319. tor_assert(mp->conf_state != PT_PROTO_INFANT);
  320. tor_assert(mp->process_handle);
  321. pos = tor_read_all_handle(tor_process_get_stdout_pipe(mp->process_handle),
  322. stdout_buf, sizeof(stdout_buf) - 1, NULL);
  323. if (pos < 0) {
  324. log_notice(LD_GENERAL, "Failed to read data from managed proxy '%s'.",
  325. mp->argv[0]);
  326. mp->conf_state = PT_PROTO_BROKEN;
  327. goto done;
  328. }
  329. if (pos == 0) /* proxy has nothing interesting to say. */
  330. return;
  331. /* End with a null even if there isn't a \r\n at the end */
  332. /* TODO: What if this is a partial line? */
  333. stdout_buf[pos] = '\0';
  334. /* Split up the buffer */
  335. lines = smartlist_new();
  336. tor_split_lines(lines, stdout_buf, pos);
  337. /* Handle lines. */
  338. SMARTLIST_FOREACH_BEGIN(lines, const char *, line) {
  339. handle_proxy_line(line, mp);
  340. if (proxy_configuration_finished(mp))
  341. goto done;
  342. } SMARTLIST_FOREACH_END(line);
  343. done:
  344. /* if the proxy finished configuring, exit the loop. */
  345. if (proxy_configuration_finished(mp))
  346. handle_finished_proxy(mp);
  347. if (lines)
  348. smartlist_free(lines);
  349. }
  350. #else /* _WIN32 */
  351. /** Attempt to continue configuring managed proxy <b>mp</b>. */
  352. static void
  353. configure_proxy(managed_proxy_t *mp)
  354. {
  355. enum stream_status r;
  356. char stdout_buf[200];
  357. /* if we haven't launched the proxy yet, do it now */
  358. if (mp->conf_state == PT_PROTO_INFANT) {
  359. if (launch_managed_proxy(mp) < 0) { /* launch fail */
  360. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  361. handle_finished_proxy(mp);
  362. }
  363. return;
  364. }
  365. tor_assert(mp->conf_state != PT_PROTO_INFANT);
  366. tor_assert(mp->process_handle);
  367. while (1) {
  368. r = get_string_from_pipe(tor_process_get_stdout_pipe(mp->process_handle),
  369. stdout_buf, sizeof(stdout_buf) - 1);
  370. if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
  371. handle_proxy_line((const char *)stdout_buf, mp);
  372. } else if (r == IO_STREAM_EAGAIN) { /* check back later */
  373. return;
  374. } else if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) { /* snap! */
  375. log_warn(LD_GENERAL, "Our communication channel with the managed proxy "
  376. "'%s' closed. Most probably application stopped running.",
  377. mp->argv[0]);
  378. mp->conf_state = PT_PROTO_BROKEN;
  379. } else { /* unknown stream status */
  380. log_warn(LD_BUG, "Unknown stream status '%d' while configuring managed "
  381. "proxy '%s'.", (int)r, mp->argv[0]);
  382. }
  383. /* if the proxy finished configuring, exit the loop. */
  384. if (proxy_configuration_finished(mp)) {
  385. handle_finished_proxy(mp);
  386. return;
  387. }
  388. }
  389. }
  390. #endif /* _WIN32 */
  391. /** Register server managed proxy <b>mp</b> transports to state */
  392. static void
  393. register_server_proxy(managed_proxy_t *mp)
  394. {
  395. /* After we register this proxy's transports, we switch its
  396. mp->transports to a list containing strings of its transport
  397. names. (See transports.h) */
  398. smartlist_t *sm_tmp = smartlist_new();
  399. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  400. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  401. save_transport_to_state(t->name, &t->addr, t->port);
  402. log_notice(LD_GENERAL, "Registered server transport '%s' at '%s:%d'",
  403. t->name, fmt_addr(&t->addr), (int)t->port);
  404. smartlist_add(sm_tmp, tor_strdup(t->name));
  405. } SMARTLIST_FOREACH_END(t);
  406. /* Since server proxies don't register their transports in the
  407. circuitbuild.c subsystem, it's our duty to free them when we
  408. switch mp->transports to strings. */
  409. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  410. smartlist_free(mp->transports);
  411. mp->transports = sm_tmp;
  412. }
  413. /** Register all the transports supported by client managed proxy
  414. * <b>mp</b> to the bridge subsystem. */
  415. static void
  416. register_client_proxy(managed_proxy_t *mp)
  417. {
  418. int r;
  419. /* After we register this proxy's transports, we switch its
  420. mp->transports to a list containing strings of its transport
  421. names. (See transports.h) */
  422. smartlist_t *sm_tmp = smartlist_new();
  423. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  424. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  425. r = transport_add(t);
  426. switch (r) {
  427. case -1:
  428. log_notice(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
  429. transport_free(t);
  430. break;
  431. case 0:
  432. log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
  433. smartlist_add(sm_tmp, tor_strdup(t->name));
  434. break;
  435. case 1:
  436. log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
  437. smartlist_add(sm_tmp, tor_strdup(t->name));
  438. transport_free(t);
  439. break;
  440. }
  441. } SMARTLIST_FOREACH_END(t);
  442. smartlist_free(mp->transports);
  443. mp->transports = sm_tmp;
  444. }
  445. /** Register the transports of managed proxy <b>mp</b>. */
  446. static INLINE void
  447. register_proxy(managed_proxy_t *mp)
  448. {
  449. if (mp->is_server)
  450. register_server_proxy(mp);
  451. else
  452. register_client_proxy(mp);
  453. }
  454. /** Free memory allocated by managed proxy <b>mp</b>. */
  455. static void
  456. managed_proxy_destroy(managed_proxy_t *mp,
  457. int also_terminate_process)
  458. {
  459. if (mp->conf_state != PT_PROTO_COMPLETED)
  460. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  461. else
  462. SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  463. /* free the transports smartlist */
  464. smartlist_free(mp->transports);
  465. /* free the transports_to_launch smartlist */
  466. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  467. smartlist_free(mp->transports_to_launch);
  468. /* remove it from the list of managed proxies */
  469. smartlist_remove(managed_proxy_list, mp);
  470. /* free the argv */
  471. free_execve_args(mp->argv);
  472. tor_process_handle_destroy(mp->process_handle, also_terminate_process);
  473. mp->process_handle = NULL;
  474. tor_free(mp);
  475. }
  476. /** Handle a configured or broken managed proxy <b>mp</b>. */
  477. static void
  478. handle_finished_proxy(managed_proxy_t *mp)
  479. {
  480. switch (mp->conf_state) {
  481. case PT_PROTO_BROKEN: /* if broken: */
  482. managed_proxy_destroy(mp, 1); /* annihilate it. */
  483. break;
  484. case PT_PROTO_FAILED_LAUNCH: /* if it failed before launching: */
  485. managed_proxy_destroy(mp, 0); /* destroy it but don't terminate */
  486. break;
  487. case PT_PROTO_CONFIGURED: /* if configured correctly: */
  488. register_proxy(mp); /* register its transports */
  489. mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
  490. break;
  491. case PT_PROTO_INFANT:
  492. case PT_PROTO_LAUNCHED:
  493. case PT_PROTO_ACCEPTING_METHODS:
  494. case PT_PROTO_COMPLETED:
  495. default:
  496. log_warn(LD_CONFIG, "Unexpected state '%d' of managed proxy '%s'.",
  497. (int)mp->conf_state, mp->argv[0]);
  498. tor_assert(0);
  499. }
  500. unconfigured_proxies_n--;
  501. tor_assert(unconfigured_proxies_n >= 0);
  502. }
  503. /** Return true if the configuration of the managed proxy <b>mp</b> is
  504. finished. */
  505. static INLINE int
  506. proxy_configuration_finished(const managed_proxy_t *mp)
  507. {
  508. return (mp->conf_state == PT_PROTO_CONFIGURED ||
  509. mp->conf_state == PT_PROTO_BROKEN ||
  510. mp->conf_state == PT_PROTO_FAILED_LAUNCH);
  511. }
  512. /** This function is called when a proxy sends an {S,C}METHODS DONE message. */
  513. static void
  514. handle_methods_done(const managed_proxy_t *mp)
  515. {
  516. tor_assert(mp->transports);
  517. if (smartlist_len(mp->transports) == 0)
  518. log_notice(LD_GENERAL, "Managed proxy '%s' was spawned successfully, "
  519. "but it didn't launch any pluggable transport listeners!",
  520. mp->argv[0]);
  521. log_info(LD_CONFIG, "%s managed proxy '%s' configuration completed!",
  522. mp->is_server ? "Server" : "Client",
  523. mp->argv[0]);
  524. }
  525. /** Handle a configuration protocol <b>line</b> received from a
  526. * managed proxy <b>mp</b>. */
  527. void
  528. handle_proxy_line(const char *line, managed_proxy_t *mp)
  529. {
  530. log_info(LD_GENERAL, "Got a line from managed proxy '%s': (%s)",
  531. mp->argv[0], line);
  532. if (!strcmpstart(line, PROTO_ENV_ERROR)) {
  533. if (mp->conf_state != PT_PROTO_LAUNCHED)
  534. goto err;
  535. parse_env_error(line);
  536. goto err;
  537. } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
  538. if (mp->conf_state != PT_PROTO_LAUNCHED)
  539. goto err;
  540. log_warn(LD_CONFIG, "Managed proxy could not pick a "
  541. "configuration protocol version.");
  542. goto err;
  543. } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
  544. if (mp->conf_state != PT_PROTO_LAUNCHED)
  545. goto err;
  546. if (parse_version(line,mp) < 0)
  547. goto err;
  548. tor_assert(mp->conf_protocol != 0);
  549. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  550. return;
  551. } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
  552. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  553. goto err;
  554. handle_methods_done(mp);
  555. mp->conf_state = PT_PROTO_CONFIGURED;
  556. return;
  557. } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
  558. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  559. goto err;
  560. handle_methods_done(mp);
  561. mp->conf_state = PT_PROTO_CONFIGURED;
  562. return;
  563. } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
  564. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  565. goto err;
  566. parse_client_method_error(line);
  567. goto err;
  568. } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
  569. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  570. goto err;
  571. parse_server_method_error(line);
  572. goto err;
  573. } else if (!strcmpstart(line, PROTO_CMETHOD)) {
  574. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  575. goto err;
  576. if (parse_cmethod_line(line, mp) < 0)
  577. goto err;
  578. return;
  579. } else if (!strcmpstart(line, PROTO_SMETHOD)) {
  580. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  581. goto err;
  582. if (parse_smethod_line(line, mp) < 0)
  583. goto err;
  584. return;
  585. } else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
  586. log_warn(LD_GENERAL, "Could not launch managed proxy executable!");
  587. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  588. return;
  589. }
  590. log_notice(LD_GENERAL, "Unknown line received by managed proxy (%s).", line);
  591. return;
  592. err:
  593. mp->conf_state = PT_PROTO_BROKEN;
  594. log_warn(LD_CONFIG, "Managed proxy at '%s' failed the configuration protocol"
  595. " and will be destroyed.", mp->argv[0]);
  596. }
  597. /** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
  598. void
  599. parse_env_error(const char *line)
  600. {
  601. /* (Length of the protocol string) plus (a space) and (the first char of
  602. the error message) */
  603. if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
  604. log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
  605. "message.", PROTO_ENV_ERROR);
  606. log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
  607. "pluggable transport environment variables. (%s)",
  608. line+strlen(PROTO_ENV_ERROR)+1);
  609. }
  610. /** Handles a VERSION <b>line</b>. Updates the configuration protocol
  611. * version in <b>mp</b>. */
  612. int
  613. parse_version(const char *line, managed_proxy_t *mp)
  614. {
  615. if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
  616. log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
  617. PROTO_NEG_SUCCESS);
  618. return -1;
  619. }
  620. if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) { /* hardcoded temp */
  621. log_warn(LD_CONFIG, "Managed proxy tried to negotiate on version '%s'. "
  622. "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
  623. return -1;
  624. }
  625. mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
  626. return 0;
  627. }
  628. /** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
  629. * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
  630. * otherwise it is a CMETHOD-ERROR. */
  631. static void
  632. parse_method_error(const char *line, int is_server)
  633. {
  634. const char* error = is_server ?
  635. PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
  636. /* (Length of the protocol string) plus (a space) and (the first char of
  637. the error message) */
  638. if (strlen(line) < (strlen(error) + 2))
  639. log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
  640. "message.", error);
  641. log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
  642. is_server ? "Server" : "Client",
  643. line+strlen(error)+1);
  644. }
  645. /** Parses an SMETHOD <b>line</b> and if well-formed it registers the
  646. * new transport in <b>mp</b>. */
  647. int
  648. parse_smethod_line(const char *line, managed_proxy_t *mp)
  649. {
  650. int r;
  651. smartlist_t *items = NULL;
  652. char *method_name=NULL;
  653. char *addrport=NULL;
  654. tor_addr_t addr;
  655. uint16_t port = 0;
  656. transport_t *transport=NULL;
  657. items = smartlist_new();
  658. smartlist_split_string(items, line, NULL,
  659. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  660. if (smartlist_len(items) < 3) {
  661. log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
  662. "with too few arguments.");
  663. goto err;
  664. }
  665. tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
  666. method_name = smartlist_get(items,1);
  667. if (!string_is_C_identifier(method_name)) {
  668. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  669. method_name);
  670. goto err;
  671. }
  672. addrport = smartlist_get(items, 2);
  673. if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
  674. log_warn(LD_CONFIG, "Error parsing transport "
  675. "address '%s'", addrport);
  676. goto err;
  677. }
  678. if (!port) {
  679. log_warn(LD_CONFIG,
  680. "Transport address '%s' has no port.", addrport);
  681. goto err;
  682. }
  683. transport = transport_new(&addr, port, method_name, PROXY_NONE);
  684. if (!transport)
  685. goto err;
  686. smartlist_add(mp->transports, transport);
  687. /* For now, notify the user so that he knows where the server
  688. transport is listening. */
  689. log_info(LD_CONFIG, "Server transport %s at %s:%d.",
  690. method_name, fmt_addr(&addr), (int)port);
  691. r=0;
  692. goto done;
  693. err:
  694. r = -1;
  695. done:
  696. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  697. smartlist_free(items);
  698. return r;
  699. }
  700. /** Parses a CMETHOD <b>line</b>, and if well-formed it registers
  701. * the new transport in <b>mp</b>. */
  702. int
  703. parse_cmethod_line(const char *line, managed_proxy_t *mp)
  704. {
  705. int r;
  706. smartlist_t *items = NULL;
  707. char *method_name=NULL;
  708. char *socks_ver_str=NULL;
  709. int socks_ver=PROXY_NONE;
  710. char *addrport=NULL;
  711. tor_addr_t addr;
  712. uint16_t port = 0;
  713. transport_t *transport=NULL;
  714. items = smartlist_new();
  715. smartlist_split_string(items, line, NULL,
  716. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  717. if (smartlist_len(items) < 4) {
  718. log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
  719. "with too few arguments.");
  720. goto err;
  721. }
  722. tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
  723. method_name = smartlist_get(items,1);
  724. if (!string_is_C_identifier(method_name)) {
  725. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  726. method_name);
  727. goto err;
  728. }
  729. socks_ver_str = smartlist_get(items,2);
  730. if (!strcmp(socks_ver_str,"socks4")) {
  731. socks_ver = PROXY_SOCKS4;
  732. } else if (!strcmp(socks_ver_str,"socks5")) {
  733. socks_ver = PROXY_SOCKS5;
  734. } else {
  735. log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
  736. "we don't recognize. (%s)", socks_ver_str);
  737. goto err;
  738. }
  739. addrport = smartlist_get(items, 3);
  740. if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
  741. log_warn(LD_CONFIG, "Error parsing transport "
  742. "address '%s'", addrport);
  743. goto err;
  744. }
  745. if (!port) {
  746. log_warn(LD_CONFIG,
  747. "Transport address '%s' has no port.", addrport);
  748. goto err;
  749. }
  750. transport = transport_new(&addr, port, method_name, socks_ver);
  751. if (!transport)
  752. goto err;
  753. smartlist_add(mp->transports, transport);
  754. log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
  755. "Attached to managed proxy.",
  756. method_name, fmt_addr(&addr), (int)port, socks_ver);
  757. r=0;
  758. goto done;
  759. err:
  760. r = -1;
  761. done:
  762. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  763. smartlist_free(items);
  764. return r;
  765. }
  766. /** Return the string that tor should place in TOR_PT_SERVER_BINDADDR
  767. * while configuring the server managed proxy in <b>mp</b>. The
  768. * string is stored in the heap, and it's the the responsibility of
  769. * the caller to deallocate it after its use. */
  770. static char *
  771. get_bindaddr_for_server_proxy(const managed_proxy_t *mp)
  772. {
  773. char *bindaddr_result = NULL;
  774. char *bindaddr_tmp = NULL;
  775. smartlist_t *string_tmp = smartlist_new();
  776. tor_assert(mp->is_server);
  777. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
  778. bindaddr_tmp = get_stored_bindaddr_for_server_transport(t);
  779. smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);
  780. tor_free(bindaddr_tmp);
  781. } SMARTLIST_FOREACH_END(t);
  782. bindaddr_result = smartlist_join_strings(string_tmp, ",", 0, NULL);
  783. SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  784. smartlist_free(string_tmp);
  785. return bindaddr_result;
  786. }
  787. /** Return a newly allocated process_environment_t * for <b>mp</b>'s
  788. * process. */
  789. static process_environment_t *
  790. create_managed_proxy_environment(const managed_proxy_t *mp)
  791. {
  792. /* Environment variables to be added to or set in mp's environment. */
  793. smartlist_t *envs = smartlist_new();
  794. /* XXXX The next time someone touches this code, shorten the name of
  795. * set_environment_variable_in_smartlist, add a
  796. * set_env_var_in_smartlist_asprintf function, and get rid of the
  797. * silly extra envs smartlist. */
  798. /* The final environment to be passed to mp. */
  799. smartlist_t *merged_env_vars = get_current_process_environment_variables();
  800. process_environment_t *env;
  801. {
  802. char *state_tmp = get_datadir_fname("pt_state/"); /* XXX temp */
  803. smartlist_add_asprintf(envs, "TOR_PT_STATE_LOCATION=%s", state_tmp);
  804. tor_free(state_tmp);
  805. }
  806. smartlist_add(envs, tor_strdup("TOR_PT_MANAGED_TRANSPORT_VER=1"));
  807. {
  808. char *transports_to_launch =
  809. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  810. smartlist_add_asprintf(envs,
  811. mp->is_server ?
  812. "TOR_PT_SERVER_TRANSPORTS=%s" :
  813. "TOR_PT_CLIENT_TRANSPORTS=%s",
  814. transports_to_launch);
  815. tor_free(transports_to_launch);
  816. }
  817. if (mp->is_server) {
  818. {
  819. char *orport_tmp =
  820. get_first_listener_addrport_string(CONN_TYPE_OR_LISTENER);
  821. smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp);
  822. tor_free(orport_tmp);
  823. }
  824. {
  825. char *bindaddr_tmp = get_bindaddr_for_server_proxy(mp);
  826. smartlist_add_asprintf(envs, "TOR_PT_SERVER_BINDADDR=%s", bindaddr_tmp);
  827. tor_free(bindaddr_tmp);
  828. }
  829. /* XXX023 Remove the '=' here once versions of obfsproxy which
  830. * assert that this env var exists are sufficiently dead.
  831. *
  832. * (If we remove this line entirely, some joker will stick this
  833. * variable in Tor's environment and crash PTs that try to parse
  834. * it even when not run in server mode.) */
  835. smartlist_add(envs, tor_strdup("TOR_PT_EXTENDED_SERVER_PORT="));
  836. }
  837. SMARTLIST_FOREACH_BEGIN(envs, const char *, env_var) {
  838. set_environment_variable_in_smartlist(merged_env_vars, env_var,
  839. _tor_free, 1);
  840. } SMARTLIST_FOREACH_END(env_var);
  841. env = process_environment_make(merged_env_vars);
  842. smartlist_free(envs);
  843. SMARTLIST_FOREACH(merged_env_vars, void *, x, tor_free(x));
  844. smartlist_free(merged_env_vars);
  845. return env;
  846. }
  847. /** Create and return a new managed proxy for <b>transport</b> using
  848. * <b>proxy_argv</b>. Also, add it to the global managed proxy list. If
  849. * <b>is_server</b> is true, it's a server managed proxy. Takes ownership of
  850. * <b>proxy_argv</b>.
  851. *
  852. * Requires that proxy_argv have at least one element. */
  853. static managed_proxy_t *
  854. managed_proxy_create(const smartlist_t *transport_list,
  855. char **proxy_argv, int is_server)
  856. {
  857. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  858. mp->conf_state = PT_PROTO_INFANT;
  859. mp->is_server = is_server;
  860. mp->argv = proxy_argv;
  861. mp->transports = smartlist_new();
  862. mp->transports_to_launch = smartlist_new();
  863. SMARTLIST_FOREACH(transport_list, const char *, transport,
  864. add_transport_to_proxy(transport, mp));
  865. /* register the managed proxy */
  866. if (!managed_proxy_list)
  867. managed_proxy_list = smartlist_new();
  868. smartlist_add(managed_proxy_list, mp);
  869. unconfigured_proxies_n++;
  870. assert_unconfigured_count_ok();
  871. return mp;
  872. }
  873. /** Register proxy with <b>proxy_argv</b>, supporting transports in
  874. * <b>transport_list</b>, to the managed proxy subsystem.
  875. * If <b>is_server</b> is true, then the proxy is a server proxy.
  876. *
  877. * Takes ownership of proxy_argv.
  878. *
  879. * Requires that proxy_argv be a NULL-terminated array of command-line
  880. * elements, containing at least one element.
  881. **/
  882. void
  883. pt_kickstart_proxy(const smartlist_t *transport_list,
  884. char **proxy_argv, int is_server)
  885. {
  886. managed_proxy_t *mp=NULL;
  887. transport_t *old_transport = NULL;
  888. if (!proxy_argv || !proxy_argv[0]) {
  889. return;
  890. }
  891. mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
  892. if (!mp) { /* we haven't seen this proxy before */
  893. managed_proxy_create(transport_list, proxy_argv, is_server);
  894. } else { /* known proxy. add its transport to its transport list */
  895. if (mp->got_hup) {
  896. /* If the managed proxy we found is marked by a SIGHUP, it means
  897. that it's not useless and should be kept. If it's marked for
  898. removal, unmark it and increase the unconfigured proxies so
  899. that we try to restart it if we need to. Afterwards, check if
  900. a transport_t for 'transport' used to exist before the SIGHUP
  901. and make sure it doesn't get deleted because we might reuse
  902. it. */
  903. if (mp->marked_for_removal) {
  904. mp->marked_for_removal = 0;
  905. check_if_restarts_needed = 1;
  906. }
  907. SMARTLIST_FOREACH_BEGIN(transport_list, const char *, transport) {
  908. old_transport = transport_get_by_name(transport);
  909. if (old_transport)
  910. old_transport->marked_for_removal = 0;
  911. } SMARTLIST_FOREACH_END(transport);
  912. }
  913. SMARTLIST_FOREACH(transport_list, const char *, transport,
  914. add_transport_to_proxy(transport, mp));
  915. free_execve_args(proxy_argv);
  916. }
  917. }
  918. /** Frees the array of pointers in <b>arg</b> used as arguments to
  919. execve(2). */
  920. static INLINE void
  921. free_execve_args(char **arg)
  922. {
  923. char **tmp = arg;
  924. while (*tmp) /* use the fact that the last element of the array is a
  925. NULL pointer to know when to stop freeing */
  926. _tor_free(*tmp++);
  927. tor_free(arg);
  928. }
  929. /** Tor will read its config.
  930. * Prepare the managed proxy list so that proxies not used in the new
  931. * config will shutdown, and proxies that need to spawn different
  932. * transports will do so. */
  933. void
  934. pt_prepare_proxy_list_for_config_read(void)
  935. {
  936. if (!managed_proxy_list)
  937. return;
  938. assert_unconfigured_count_ok();
  939. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  940. /* Destroy unconfigured proxies. */
  941. if (mp->conf_state != PT_PROTO_COMPLETED) {
  942. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  943. managed_proxy_destroy(mp, 1);
  944. unconfigured_proxies_n--;
  945. continue;
  946. }
  947. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  948. mp->marked_for_removal = 1;
  949. mp->got_hup = 1;
  950. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  951. smartlist_clear(mp->transports_to_launch);
  952. } SMARTLIST_FOREACH_END(mp);
  953. assert_unconfigured_count_ok();
  954. tor_assert(unconfigured_proxies_n == 0);
  955. }
  956. /** The tor config was read.
  957. * Destroy all managed proxies that were marked by a previous call to
  958. * prepare_proxy_list_for_config_read() and are not used by the new
  959. * config. */
  960. void
  961. sweep_proxy_list(void)
  962. {
  963. if (!managed_proxy_list)
  964. return;
  965. assert_unconfigured_count_ok();
  966. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  967. if (mp->marked_for_removal) {
  968. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  969. managed_proxy_destroy(mp, 1);
  970. }
  971. } SMARTLIST_FOREACH_END(mp);
  972. assert_unconfigured_count_ok();
  973. }
  974. /** Release all storage held by the pluggable transports subsystem. */
  975. void
  976. pt_free_all(void)
  977. {
  978. if (managed_proxy_list) {
  979. /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
  980. transports and it's the duty of the circuitbuild.c subsystem to
  981. free them. Otherwise, it hasn't registered its transports yet
  982. and we should free them here. */
  983. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
  984. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  985. managed_proxy_destroy(mp, 1);
  986. });
  987. smartlist_free(managed_proxy_list);
  988. managed_proxy_list=NULL;
  989. }
  990. }