transports.c 31 KB

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