transports.c 30 KB

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