transports.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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, const managed_proxy_t *mp);
  14. static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
  15. static void managed_proxy_destroy(managed_proxy_t *mp);
  16. static void handle_finished_proxy(managed_proxy_t *mp);
  17. static void configure_proxy(managed_proxy_t *mp);
  18. static void parse_method_error(const char *line, int is_server_method);
  19. #define parse_server_method_error(l) parse_method_error(l, 1)
  20. #define parse_client_method_error(l) parse_method_error(l, 0)
  21. static INLINE void free_execve_args(char **arg);
  22. /** Managed proxy protocol strings */
  23. #define PROTO_ENV_ERROR "ENV-ERROR"
  24. #define PROTO_NEG_SUCCESS "VERSION"
  25. #define PROTO_NEG_FAIL "VERSION-ERROR no-version"
  26. #define PROTO_CMETHOD "CMETHOD"
  27. #define PROTO_SMETHOD "SMETHOD"
  28. #define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
  29. #define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
  30. #define PROTO_CMETHODS_DONE "CMETHODS DONE"
  31. #define PROTO_SMETHODS_DONE "SMETHODS DONE"
  32. /* The smallest valid managed proxy protocol line that can
  33. appear. It's the size of "VERSION 1" */
  34. #define SMALLEST_MANAGED_LINE_SIZE 9
  35. /** Number of environment variables for managed proxy clients/servers. */
  36. #define ENVIRON_SIZE_CLIENT 5
  37. #define ENVIRON_SIZE_SERVER 8
  38. /** The first and only supported - at the moment - configuration
  39. protocol version. */
  40. #define PROTO_VERSION_ONE 1
  41. /** List of unconfigured managed proxies. */
  42. static smartlist_t *managed_proxy_list = NULL;
  43. /** Number of still unconfigured proxies. */
  44. static int unconfigured_proxies_n = 0;
  45. /** "The main idea is:"
  46. Each managed proxy is represented by a 'managed_proxy_t'.
  47. Each managed proxy can support multiple transports.
  48. Each managed proxy gets configured through a multistep process.
  49. 'managed_proxy_list' contains all the managed proxies this tor
  50. instance is supporting.
  51. In the 'managed_proxy_list' there are 'unconfigured_proxies_n'
  52. managed proxies that are still unconfigured.
  53. In every run_scheduled_event() tick, we attempt to launch and then
  54. configure the unconfiged managed proxies, using the configuration
  55. protocol defined in the 180_pluggable_transport.txt proposal. A
  56. managed proxy might need several ticks to get fully configured.
  57. When a managed proxy is fully configured, we register all its
  58. transports to the circuitbuild.c subsystem. At that point the
  59. transports are owned by the circuitbuild.c subsystem.
  60. When a managed proxy fails to follow the 180 configuration
  61. protocol, it gets marked as broken and gets destroyed.
  62. "In a little more technical detail:"
  63. While we are serially parsing torrc, we store all the transports
  64. that a proxy should spawn in its 'transports_to_launch' element.
  65. When we finish reading the torrc, we spawn the managed proxy and
  66. expect {S,C}METHOD lines from its output. We add transports
  67. described by METHOD lines to its 'transports' element, as
  68. 'transport_t' structs.
  69. When the managed proxy stops spitting METHOD lines (signified by a
  70. '{S,C}METHODS DONE' message) we register all the transports
  71. collected to the circuitbuild.c subsystem. At this point, the
  72. 'transport_t's can be transformed into dangling pointers at any
  73. point by the circuitbuild.c subsystem, and so we replace all
  74. 'transport_t's with strings describing the transport names. We
  75. can still go from a transport name to a 'transport_t' using the
  76. fact that transport names uniquely identify 'transport_t's.
  77. "In even more technical detail I shall describe what happens when
  78. the SIGHUP bell tolls:"
  79. We immediately destroy all unconfigured proxies (We shouldn't have
  80. unconfigured proxies in the first place, except when SIGHUP rings
  81. immediately after tor is launched.).
  82. We mark all managed proxies and transports to signify that they
  83. must be removed if they don't contribute by the new torrc. We also
  84. mark all managed proxies to signify that they might need to be
  85. restarted so that they end up supporting all the transports the
  86. new torrc wants them to support.
  87. We also clear the 'transports_to_launch' list so that we can put
  88. there the transports we need to launch on each proxy according to
  89. the new torrc.
  90. We then start parsing torrc again, everytime we encounter a
  91. transport line using a known pre-SIGHUP managed proxy, we cleanse
  92. that proxy from the removal mark.
  93. We also mark it as unconfigured so that on the next scheduled
  94. events tick, we investigate whether we need to restart the proxy
  95. so that it spawns the new 'transport_to_launch' list. Of course,
  96. if the post-SIGHUP 'transports_to_launch' list is identical to the
  97. pre-SIGHUP one, it means that no changes were introduced to this
  98. proxy during the SIGHUP and no restart has to take place.
  99. During the post-SIGHUP torrc parsing, we unmark all transports we
  100. encounter. This happens in the case that no restart is needed, we
  101. can continue using the old transports normally. If we end up
  102. restarting the proxy, we destroy and unregister all old transports
  103. from the circuitbuild.c subsystem since they become useless.
  104. */
  105. /** Return true if there are still unconfigured managed proxies. */
  106. int
  107. pt_proxies_configuration_pending(void)
  108. {
  109. return !! unconfigured_proxies_n;
  110. }
  111. /** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
  112. static int
  113. managed_proxy_has_argv(managed_proxy_t *mp, char **proxy_argv)
  114. {
  115. char **tmp1=proxy_argv;
  116. char **tmp2=mp->argv;
  117. tor_assert(tmp1);
  118. tor_assert(tmp2);
  119. while (*tmp1 && *tmp2) {
  120. if (strcmp(*tmp1++, *tmp2++))
  121. return 0;
  122. }
  123. if (!*tmp1 && !*tmp2)
  124. return 1;
  125. return 0;
  126. }
  127. /** Return a managed proxy with the same argv as <b>proxy_argv</b>.
  128. * If no such managed proxy exists, return NULL. */
  129. static managed_proxy_t *
  130. get_managed_proxy_by_argv_and_type(char **proxy_argv, int is_server)
  131. {
  132. if (!managed_proxy_list)
  133. return NULL;
  134. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  135. if (managed_proxy_has_argv(mp, proxy_argv) &&
  136. mp->is_server == is_server)
  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);
  300. smartlist_add(sm_tmp, tor_strdup(t->name));
  301. } SMARTLIST_FOREACH_END(t);
  302. /* Since server proxies don't register their transports in the
  303. circuitbuild.c subsystem, it's our duty to free them when we
  304. switch mp->transports to strings. */
  305. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  306. smartlist_free(mp->transports);
  307. mp->transports = sm_tmp;
  308. }
  309. /** Register all the transports supported by client managed proxy
  310. * <b>mp</b> to the bridge subsystem. */
  311. static void
  312. register_client_proxy(managed_proxy_t *mp)
  313. {
  314. int r;
  315. smartlist_t *sm_tmp = smartlist_create();
  316. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  317. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  318. r = transport_add(t);
  319. switch (r) {
  320. case -1:
  321. log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
  322. transport_free(t);
  323. break;
  324. case 0:
  325. log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
  326. smartlist_add(sm_tmp, tor_strdup(t->name));
  327. break;
  328. case 1:
  329. log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
  330. smartlist_add(sm_tmp, tor_strdup(t->name));
  331. transport_free(t);
  332. break;
  333. }
  334. } SMARTLIST_FOREACH_END(t);
  335. smartlist_free(mp->transports);
  336. mp->transports = sm_tmp;
  337. }
  338. /** Register the transports of managed proxy <b>mp</b>. */
  339. static INLINE void
  340. register_proxy(managed_proxy_t *mp)
  341. {
  342. if (mp->is_server)
  343. register_server_proxy(mp);
  344. else
  345. register_client_proxy(mp);
  346. }
  347. /** Free memory allocated by managed proxy <b>mp</b>. */
  348. static void
  349. managed_proxy_destroy(managed_proxy_t *mp)
  350. {
  351. printf("Destroying mp %p\n", mp);
  352. if (mp->conf_state != PT_PROTO_COMPLETED)
  353. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  354. else
  355. SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  356. /* free the transports smartlist */
  357. smartlist_free(mp->transports);
  358. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  359. /* free the transports smartlist */
  360. smartlist_free(mp->transports_to_launch);
  361. /* remove it from the list of managed proxies */
  362. smartlist_remove(managed_proxy_list, mp);
  363. /* close its stdout stream */
  364. if (mp->stdout)
  365. fclose(mp->stdout);
  366. /* free the argv */
  367. free_execve_args(mp->argv);
  368. if (mp->pid)
  369. tor_terminate_process(mp->pid);
  370. tor_free(mp);
  371. }
  372. /** Handle a configured or broken managed proxy <b>mp</b>. */
  373. static void
  374. handle_finished_proxy(managed_proxy_t *mp)
  375. {
  376. switch (mp->conf_state) {
  377. case PT_PROTO_BROKEN: /* if broken: */
  378. managed_proxy_destroy(mp); /* annihilate it. */
  379. break;
  380. case PT_PROTO_CONFIGURED: /* if configured correctly: */
  381. register_proxy(mp); /* register transports */
  382. mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed. */
  383. break;
  384. case PT_PROTO_INFANT:
  385. case PT_PROTO_LAUNCHED:
  386. case PT_PROTO_ACCEPTING_METHODS:
  387. case PT_PROTO_COMPLETED:
  388. default:
  389. log_warn(LD_CONFIG, "Unexpected managed proxy state in "
  390. "handle_finished_proxy().");
  391. tor_assert(0);
  392. }
  393. unconfigured_proxies_n--;
  394. tor_assert(unconfigured_proxies_n >= 0);
  395. }
  396. /** Return true if the configuration of the managed proxy <b>mp</b> is
  397. finished. */
  398. static INLINE int
  399. proxy_configuration_finished(const managed_proxy_t *mp)
  400. {
  401. return (mp->conf_state == PT_PROTO_CONFIGURED ||
  402. mp->conf_state == PT_PROTO_BROKEN);
  403. }
  404. /** This function is called when a proxy sends an {S,C}METHODS DONE message. */
  405. static void
  406. handle_methods_done(const managed_proxy_t *mp)
  407. {
  408. tor_assert(mp->transports);
  409. if (smartlist_len(mp->transports) == 0)
  410. log_warn(LD_GENERAL, "Proxy was spawned successfully, "
  411. "but it didn't laucn any pluggable transport listeners!");
  412. log_warn(LD_CONFIG, "%s managed proxy configuration completed!",
  413. mp->is_server ? "Server" : "Client");
  414. }
  415. /** Handle a configuration protocol <b>line</b> received from a
  416. * managed proxy <b>mp</b>. */
  417. void
  418. handle_proxy_line(const char *line, managed_proxy_t *mp)
  419. {
  420. printf("Judging line: %s\n", line);
  421. if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
  422. log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
  423. "Discarding");
  424. goto err;
  425. }
  426. if (!strcmpstart(line, PROTO_ENV_ERROR)) {
  427. if (mp->conf_state != PT_PROTO_LAUNCHED)
  428. goto err;
  429. parse_env_error(line);
  430. goto err;
  431. } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
  432. if (mp->conf_state != PT_PROTO_LAUNCHED)
  433. goto err;
  434. log_warn(LD_CONFIG, "Managed proxy could not pick a "
  435. "configuration protocol version.");
  436. goto err;
  437. } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
  438. if (mp->conf_state != PT_PROTO_LAUNCHED)
  439. goto err;
  440. if (parse_version(line,mp) < 0)
  441. goto err;
  442. tor_assert(mp->conf_protocol != 0);
  443. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  444. return;
  445. } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
  446. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  447. goto err;
  448. handle_methods_done(mp);
  449. mp->conf_state = PT_PROTO_CONFIGURED;
  450. return;
  451. } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
  452. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  453. goto err;
  454. handle_methods_done(mp);
  455. mp->conf_state = PT_PROTO_CONFIGURED;
  456. return;
  457. } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
  458. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  459. goto err;
  460. parse_client_method_error(line);
  461. goto err;
  462. } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
  463. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  464. goto err;
  465. parse_server_method_error(line);
  466. goto err;
  467. } else if (!strcmpstart(line, PROTO_CMETHOD)) {
  468. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  469. goto err;
  470. if (parse_cmethod_line(line, mp) < 0)
  471. goto err;
  472. return;
  473. } else if (!strcmpstart(line, PROTO_SMETHOD)) {
  474. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  475. goto err;
  476. if (parse_smethod_line(line, mp) < 0)
  477. goto err;
  478. return;
  479. } else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
  480. log_warn(LD_GENERAL, "Could not launch managed proxy executable!");
  481. goto err;
  482. }
  483. log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
  484. err:
  485. mp->conf_state = PT_PROTO_BROKEN;
  486. return;
  487. }
  488. /** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
  489. void
  490. parse_env_error(const char *line)
  491. {
  492. /* (Length of the protocol string) plus (a space) and (the first char of
  493. the error message) */
  494. if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
  495. log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
  496. "message.", PROTO_ENV_ERROR);
  497. log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
  498. "pluggable transport environment variables. (%s)",
  499. line+strlen(PROTO_ENV_ERROR)+1);
  500. }
  501. /** Handles a VERSION <b>line</b>. Updates the configuration protocol
  502. * version in <b>mp</b>. */
  503. int
  504. parse_version(const char *line, managed_proxy_t *mp)
  505. {
  506. if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
  507. log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
  508. PROTO_NEG_SUCCESS);
  509. return -1;
  510. }
  511. if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) {
  512. log_warn(LD_CONFIG, "We don't support version '%s'. "
  513. "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
  514. return -1;
  515. }
  516. mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
  517. return 0;
  518. }
  519. /** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
  520. * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
  521. * otherwise it is a CMETHOD-ERROR. */
  522. static void
  523. parse_method_error(const char *line, int is_server)
  524. {
  525. const char* error = is_server ?
  526. PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
  527. /* (Length of the protocol string) plus (a space) and (the first char of
  528. the error message) */
  529. if (strlen(line) < (strlen(error) + 2))
  530. log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
  531. "message.", error);
  532. log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
  533. is_server ? "Server" : "Client",
  534. line+strlen(error)+1);
  535. }
  536. /** Parses an SMETHOD <b>line</b> and if well-formed it registers the
  537. * new transport in <b>mp</b>. */
  538. int
  539. parse_smethod_line(const char *line, managed_proxy_t *mp)
  540. {
  541. int r;
  542. smartlist_t *items = NULL;
  543. char *method_name=NULL;
  544. char *addrport=NULL;
  545. tor_addr_t addr;
  546. uint16_t port = 0;
  547. transport_t *transport=NULL;
  548. items = smartlist_create();
  549. smartlist_split_string(items, line, NULL,
  550. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  551. if (smartlist_len(items) < 3) {
  552. log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
  553. "with too few arguments.");
  554. goto err;
  555. }
  556. tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
  557. method_name = smartlist_get(items,1);
  558. if (!string_is_C_identifier(method_name)) {
  559. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  560. method_name);
  561. goto err;
  562. }
  563. addrport = smartlist_get(items, 2);
  564. if (tor_addr_port_parse(addrport, &addr, &port)<0) {
  565. log_warn(LD_CONFIG, "Error parsing transport "
  566. "address '%s'", addrport);
  567. goto err;
  568. }
  569. if (!port) {
  570. log_warn(LD_CONFIG,
  571. "Transport address '%s' has no port.", addrport);
  572. goto err;
  573. }
  574. transport = transport_create(&addr, port, method_name, PROXY_NONE);
  575. if (!transport)
  576. goto err;
  577. smartlist_add(mp->transports, transport);
  578. /* For now, notify the user so that he knows where the server
  579. transport is listening. */
  580. log_warn(LD_CONFIG, "Server transport %s at %s:%d.",
  581. method_name, fmt_addr(&addr), (int)port);
  582. r=0;
  583. goto done;
  584. err:
  585. r = -1;
  586. done:
  587. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  588. smartlist_free(items);
  589. return r;
  590. }
  591. /** Parses a CMETHOD <b>line</b>, and if well-formed it registers
  592. * the new transport in <b>mp</b>. */
  593. int
  594. parse_cmethod_line(const char *line, managed_proxy_t *mp)
  595. {
  596. int r;
  597. smartlist_t *items = NULL;
  598. char *method_name=NULL;
  599. char *socks_ver_str=NULL;
  600. int socks_ver=PROXY_NONE;
  601. char *addrport=NULL;
  602. tor_addr_t addr;
  603. uint16_t port = 0;
  604. transport_t *transport=NULL;
  605. items = smartlist_create();
  606. smartlist_split_string(items, line, NULL,
  607. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  608. if (smartlist_len(items) < 4) {
  609. log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
  610. "with too few arguments.");
  611. goto err;
  612. }
  613. tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
  614. method_name = smartlist_get(items,1);
  615. if (!string_is_C_identifier(method_name)) {
  616. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  617. method_name);
  618. goto err;
  619. }
  620. socks_ver_str = smartlist_get(items,2);
  621. if (!strcmp(socks_ver_str,"socks4")) {
  622. socks_ver = PROXY_SOCKS4;
  623. } else if (!strcmp(socks_ver_str,"socks5")) {
  624. socks_ver = PROXY_SOCKS5;
  625. } else {
  626. log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
  627. "we don't recognize. (%s)", socks_ver_str);
  628. goto err;
  629. }
  630. addrport = smartlist_get(items, 3);
  631. if (tor_addr_port_parse(addrport, &addr, &port)<0) {
  632. log_warn(LD_CONFIG, "Error parsing transport "
  633. "address '%s'", addrport);
  634. goto err;
  635. }
  636. if (!port) {
  637. log_warn(LD_CONFIG,
  638. "Transport address '%s' has no port.", addrport);
  639. goto err;
  640. }
  641. transport = transport_create(&addr, port, method_name, socks_ver);
  642. if (!transport)
  643. goto err;
  644. smartlist_add(mp->transports, transport);
  645. log_warn(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
  646. "Attached to managed proxy.",
  647. method_name, fmt_addr(&addr), (int)port, socks_ver);
  648. r=0;
  649. goto done;
  650. err:
  651. r = -1;
  652. done:
  653. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  654. smartlist_free(items);
  655. return r;
  656. }
  657. /** Return a string containing the address:port that <b>transport</b>
  658. * should use. It's the responsibility of the caller to free() the
  659. * received string. */
  660. static char *
  661. get_bindaddr_for_proxy(const managed_proxy_t *mp)
  662. {
  663. char *bindaddr = NULL;
  664. smartlist_t *string_tmp = smartlist_create();
  665. tor_assert(mp->is_server);
  666. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
  667. tor_asprintf(&bindaddr, "%s-%s", t, get_bindaddr_for_transport(t));
  668. smartlist_add(string_tmp, bindaddr);
  669. } SMARTLIST_FOREACH_END(t);
  670. bindaddr = smartlist_join_strings(string_tmp, ",", 0, NULL);
  671. SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  672. smartlist_free(string_tmp);
  673. return bindaddr;
  674. }
  675. /** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
  676. static void
  677. set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
  678. {
  679. or_options_t *options = get_options();
  680. char **tmp=NULL;
  681. char *state_loc=NULL;
  682. char *transports_to_launch=NULL;
  683. char *bindaddr=NULL;
  684. int n_envs = mp->is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
  685. /* allocate enough space for our env. vars and a NULL pointer */
  686. *envp = tor_malloc(sizeof(char*)*(n_envs+1));
  687. tmp = *envp;
  688. state_loc = get_datadir_fname("pt_state/"); /* XXX temp */
  689. transports_to_launch =
  690. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  691. tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
  692. tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
  693. tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
  694. tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
  695. if (mp->is_server) {
  696. bindaddr = get_bindaddr_for_proxy(mp);
  697. tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* XXX temp */
  698. tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
  699. tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
  700. tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* XXX temp*/
  701. } else {
  702. tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
  703. }
  704. *tmp = NULL;
  705. tor_free(state_loc);
  706. tor_free(transports_to_launch);
  707. tor_free(bindaddr);
  708. }
  709. /** Create and return a new managed proxy for <b>transport</b> using
  710. * <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
  711. * managed proxy. */
  712. static managed_proxy_t *
  713. managed_proxy_create(const char *transport, char **proxy_argv, int is_server)
  714. {
  715. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  716. mp->conf_state = PT_PROTO_INFANT;
  717. mp->is_server = is_server;
  718. mp->argv = proxy_argv;
  719. mp->transports = smartlist_create();
  720. mp->transports_to_launch = smartlist_create();
  721. add_transport_to_proxy(transport, mp);
  722. /* register the managed proxy */
  723. if (!managed_proxy_list)
  724. managed_proxy_list = smartlist_create();
  725. smartlist_add(managed_proxy_list, mp);
  726. unconfigured_proxies_n++;
  727. return mp;
  728. }
  729. /** Register <b>transport</b> using proxy with <b>proxy_argv</b> to
  730. * the managed proxy subsystem.
  731. * If <b>is_server</b> is true, then the proxy is a server proxy. */
  732. void
  733. pt_kickstart_proxy(const char *transport, char **proxy_argv, int is_server)
  734. {
  735. managed_proxy_t *mp=NULL;
  736. transport_t *old_transport = NULL;
  737. mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
  738. if (!mp) { /* we haven't seen this proxy before */
  739. managed_proxy_create(transport, proxy_argv, is_server);
  740. } else { /* known proxy. add its transport to its transport list */
  741. if (mp->got_hup) {
  742. /* If the managed proxy we found is marked by a SIGHUP, it means
  743. that it's not useless and should be kept. If it's marked for
  744. removal, unmark it and increase the unconfigured proxies so
  745. that we try to restart it if we need to. Afterwards, check if
  746. a transport_t for 'transport' used to exist before the SIGHUP
  747. and make sure it doesn't get deleted because we might reuse
  748. it. */
  749. if (mp->marked_for_removal) {
  750. mp->marked_for_removal = 0;
  751. unconfigured_proxies_n++;
  752. }
  753. old_transport = transport_get_by_name(transport);
  754. if (old_transport)
  755. old_transport->marked_for_removal = 0;
  756. }
  757. add_transport_to_proxy(transport, mp);
  758. free_execve_args(proxy_argv);
  759. }
  760. }
  761. /** Frees the array of pointers in <b>arg</b> used as arguments to
  762. execve. */
  763. static INLINE void
  764. free_execve_args(char **arg)
  765. {
  766. char **tmp = arg;
  767. while (*tmp) /* use the fact that the last element of the array is a
  768. NULL pointer to know when to stop freeing */
  769. _tor_free(*tmp++);
  770. tor_free(arg);
  771. }
  772. /** Tor will read its config, prepare the managed proxy list so that
  773. * proxies that are not used in the new config will shutdown, and
  774. * proxies that need to spawn more transports will do so. */
  775. void
  776. pt_prepare_proxy_list_for_config_read(void)
  777. {
  778. if (!managed_proxy_list)
  779. return;
  780. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  781. /* Destroy unconfigured proxies. */
  782. if (mp->conf_state != PT_PROTO_COMPLETED) {
  783. managed_proxy_destroy(mp);
  784. unconfigured_proxies_n--;
  785. continue;
  786. }
  787. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  788. mp->marked_for_removal = 1;
  789. mp->got_hup = 1;
  790. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  791. smartlist_clear(mp->transports_to_launch);
  792. } SMARTLIST_FOREACH_END(mp);
  793. tor_assert(unconfigured_proxies_n == 0);
  794. }
  795. /** The tor config was read, destroy all managed proxies that were
  796. * marked by a previous call to prepare_proxy_list_for_config_read()
  797. * and are not used by the new config. */
  798. void
  799. sweep_proxy_list(void)
  800. {
  801. if (!managed_proxy_list)
  802. return;
  803. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  804. if (mp->marked_for_removal) {
  805. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  806. managed_proxy_destroy(mp);
  807. }
  808. } SMARTLIST_FOREACH_END(mp);
  809. }
  810. /** Release all storage held by the pluggable transports subsystem. */
  811. void
  812. pt_free_all(void)
  813. {
  814. if (managed_proxy_list) {
  815. /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
  816. transports and it's the duty of the circuitbuild.c subsystem to
  817. free them. Otherwise, it hasn't registered its transports yet
  818. and we should free them here. */
  819. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp,
  820. managed_proxy_destroy(mp));
  821. smartlist_free(managed_proxy_list);
  822. managed_proxy_list=NULL;
  823. }
  824. }