transports.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. (void) mp;
  206. (void) set_managed_proxy_environment;
  207. return -1;
  208. #if 0
  209. /* XXXX023 we must reenable this code for managed proxies to work.
  210. * "All it needs" is revision to work with the new tor_spawn_background
  211. * API. */
  212. char **envp=NULL;
  213. int pid;
  214. process_handle_t proc;
  215. FILE *stdout_read = NULL;
  216. int stdout_pipe=-1, stderr_pipe=-1;
  217. /* prepare the environment variables for the managed proxy */
  218. set_managed_proxy_environment(&envp, mp);
  219. pid = tor_spawn_background(mp->argv[0], (const char **)mp->argv,
  220. (const char **)envp, &proc);
  221. if (pid < 0) {
  222. log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
  223. mp->argv[0]);
  224. return -1;
  225. }
  226. /* free the memory allocated by set_managed_proxy_environment(). */
  227. free_execve_args(envp);
  228. /* Set stdout/stderr pipes to be non-blocking */
  229. #ifdef _WIN32
  230. {
  231. u_long nonblocking = 1;
  232. ioctlsocket(stdout_pipe, FIONBIO, &nonblocking);
  233. }
  234. #else
  235. fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
  236. #endif
  237. /* Open the buffered IO streams */
  238. stdout_read = fdopen(stdout_pipe, "r");
  239. log_info(LD_CONFIG, "Managed proxy has spawned at PID %d.", pid);
  240. mp->conf_state = PT_PROTO_LAUNCHED;
  241. mp->_stdout = stdout_read;
  242. mp->pid = pid;
  243. #endif
  244. return 0;
  245. }
  246. /** Check if any of the managed proxies we are currently trying to
  247. * configure have anything new to say. This is called from
  248. * run_scheduled_events(). */
  249. void
  250. pt_configure_remaining_proxies(void)
  251. {
  252. log_debug(LD_CONFIG, "Configuring remaining managed proxies (%d)!",
  253. unconfigured_proxies_n);
  254. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  255. tor_assert(mp->conf_state != PT_PROTO_BROKEN);
  256. if (mp->got_hup) {
  257. mp->got_hup = 0;
  258. /* This proxy is marked by a SIGHUP. Check whether we need to
  259. restart it. */
  260. if (proxy_needs_restart(mp)) {
  261. log_info(LD_GENERAL, "Preparing managed proxy for restart.");
  262. proxy_prepare_for_restart(mp);
  263. continue;
  264. } else { /* it doesn't need to be restarted. */
  265. log_info(LD_GENERAL, "Nothing changed for managed proxy after HUP: "
  266. "not restarting.");
  267. unconfigured_proxies_n--;
  268. tor_assert(unconfigured_proxies_n >= 0);
  269. }
  270. continue;
  271. }
  272. /* If the proxy is not fully configured, try to configure it
  273. futher. */
  274. if (!proxy_configuration_finished(mp))
  275. configure_proxy(mp);
  276. } SMARTLIST_FOREACH_END(mp);
  277. }
  278. /** Attempt to continue configuring managed proxy <b>mp</b>. */
  279. static void
  280. configure_proxy(managed_proxy_t *mp)
  281. {
  282. enum stream_status r;
  283. char stdout_buf[200];
  284. /* if we haven't launched the proxy yet, do it now */
  285. if (mp->conf_state == PT_PROTO_INFANT) {
  286. launch_managed_proxy(mp);
  287. return;
  288. }
  289. tor_assert(mp->conf_state != PT_PROTO_INFANT);
  290. while (1) {
  291. r = get_string_from_pipe(mp->_stdout, stdout_buf,
  292. sizeof(stdout_buf) - 1);
  293. if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
  294. handle_proxy_line((const char *)stdout_buf, mp);
  295. } else if (r == IO_STREAM_EAGAIN) { /* check back later */
  296. return;
  297. } else if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) { /* snap! */
  298. log_notice(LD_GENERAL, "Managed proxy stream closed. "
  299. "Most probably application stopped running");
  300. mp->conf_state = PT_PROTO_BROKEN;
  301. } else { /* unknown stream status */
  302. log_notice(LD_GENERAL, "Unknown stream status while configuring proxy.");
  303. }
  304. /* if the proxy finished configuring, exit the loop. */
  305. if (proxy_configuration_finished(mp)) {
  306. handle_finished_proxy(mp);
  307. return;
  308. }
  309. }
  310. }
  311. /** Register server managed proxy <b>mp</b> transports to state */
  312. static void
  313. register_server_proxy(managed_proxy_t *mp)
  314. {
  315. /* After we register this proxy's transports, we switch its
  316. mp->transports to a list containing strings of its transport
  317. names. (See transports.h) */
  318. smartlist_t *sm_tmp = smartlist_create();
  319. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  320. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  321. save_transport_to_state(t->name, &t->addr, t->port);
  322. smartlist_add(sm_tmp, tor_strdup(t->name));
  323. } SMARTLIST_FOREACH_END(t);
  324. /* Since server proxies don't register their transports in the
  325. circuitbuild.c subsystem, it's our duty to free them when we
  326. switch mp->transports to strings. */
  327. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  328. smartlist_free(mp->transports);
  329. mp->transports = sm_tmp;
  330. }
  331. /** Register all the transports supported by client managed proxy
  332. * <b>mp</b> to the bridge subsystem. */
  333. static void
  334. register_client_proxy(managed_proxy_t *mp)
  335. {
  336. int r;
  337. /* After we register this proxy's transports, we switch its
  338. mp->transports to a list containing strings of its transport
  339. names. (See transports.h) */
  340. smartlist_t *sm_tmp = smartlist_create();
  341. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  342. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  343. r = transport_add(t);
  344. switch (r) {
  345. case -1:
  346. log_notice(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
  347. transport_free(t);
  348. break;
  349. case 0:
  350. log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
  351. smartlist_add(sm_tmp, tor_strdup(t->name));
  352. break;
  353. case 1:
  354. log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
  355. smartlist_add(sm_tmp, tor_strdup(t->name));
  356. transport_free(t);
  357. break;
  358. }
  359. } SMARTLIST_FOREACH_END(t);
  360. smartlist_free(mp->transports);
  361. mp->transports = sm_tmp;
  362. }
  363. /** Register the transports of managed proxy <b>mp</b>. */
  364. static INLINE void
  365. register_proxy(managed_proxy_t *mp)
  366. {
  367. if (mp->is_server)
  368. register_server_proxy(mp);
  369. else
  370. register_client_proxy(mp);
  371. }
  372. /** Free memory allocated by managed proxy <b>mp</b>. */
  373. static void
  374. managed_proxy_destroy(managed_proxy_t *mp)
  375. {
  376. if (mp->conf_state != PT_PROTO_COMPLETED)
  377. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  378. else
  379. SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  380. /* free the transports smartlist */
  381. smartlist_free(mp->transports);
  382. /* free the transports_to_launch smartlist */
  383. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  384. smartlist_free(mp->transports_to_launch);
  385. /* remove it from the list of managed proxies */
  386. smartlist_remove(managed_proxy_list, mp);
  387. /* close its stdout stream */
  388. if (mp->_stdout)
  389. fclose(mp->_stdout);
  390. /* free the argv */
  391. free_execve_args(mp->argv);
  392. if (mp->pid)
  393. tor_terminate_process(mp->pid);
  394. tor_free(mp);
  395. }
  396. /** Handle a configured or broken managed proxy <b>mp</b>. */
  397. static void
  398. handle_finished_proxy(managed_proxy_t *mp)
  399. {
  400. switch (mp->conf_state) {
  401. case PT_PROTO_BROKEN: /* if broken: */
  402. managed_proxy_destroy(mp); /* annihilate it. */
  403. break;
  404. case PT_PROTO_CONFIGURED: /* if configured correctly: */
  405. register_proxy(mp); /* register its transports */
  406. mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
  407. break;
  408. case PT_PROTO_INFANT:
  409. case PT_PROTO_LAUNCHED:
  410. case PT_PROTO_ACCEPTING_METHODS:
  411. case PT_PROTO_COMPLETED:
  412. default:
  413. log_warn(LD_CONFIG, "Unexpected managed proxy state in "
  414. "handle_finished_proxy().");
  415. tor_assert(0);
  416. }
  417. unconfigured_proxies_n--;
  418. tor_assert(unconfigured_proxies_n >= 0);
  419. }
  420. /** Return true if the configuration of the managed proxy <b>mp</b> is
  421. finished. */
  422. static INLINE int
  423. proxy_configuration_finished(const managed_proxy_t *mp)
  424. {
  425. return (mp->conf_state == PT_PROTO_CONFIGURED ||
  426. mp->conf_state == PT_PROTO_BROKEN);
  427. }
  428. /** This function is called when a proxy sends an {S,C}METHODS DONE message. */
  429. static void
  430. handle_methods_done(const managed_proxy_t *mp)
  431. {
  432. tor_assert(mp->transports);
  433. if (smartlist_len(mp->transports) == 0)
  434. log_notice(LD_GENERAL, "Proxy was spawned successfully, "
  435. "but it didn't laucn any pluggable transport listeners!");
  436. log_info(LD_CONFIG, "%s managed proxy configuration completed!",
  437. mp->is_server ? "Server" : "Client");
  438. }
  439. /** Handle a configuration protocol <b>line</b> received from a
  440. * managed proxy <b>mp</b>. */
  441. void
  442. handle_proxy_line(const char *line, managed_proxy_t *mp)
  443. {
  444. log_debug(LD_GENERAL, "Got a line from managed proxy: %s\n", line);
  445. if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
  446. log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
  447. "Discarding");
  448. goto err;
  449. }
  450. if (!strcmpstart(line, PROTO_ENV_ERROR)) {
  451. if (mp->conf_state != PT_PROTO_LAUNCHED)
  452. goto err;
  453. parse_env_error(line);
  454. goto err;
  455. } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
  456. if (mp->conf_state != PT_PROTO_LAUNCHED)
  457. goto err;
  458. log_warn(LD_CONFIG, "Managed proxy could not pick a "
  459. "configuration protocol version.");
  460. goto err;
  461. } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
  462. if (mp->conf_state != PT_PROTO_LAUNCHED)
  463. goto err;
  464. if (parse_version(line,mp) < 0)
  465. goto err;
  466. tor_assert(mp->conf_protocol != 0);
  467. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  468. return;
  469. } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
  470. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  471. goto err;
  472. handle_methods_done(mp);
  473. mp->conf_state = PT_PROTO_CONFIGURED;
  474. return;
  475. } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
  476. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  477. goto err;
  478. handle_methods_done(mp);
  479. mp->conf_state = PT_PROTO_CONFIGURED;
  480. return;
  481. } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
  482. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  483. goto err;
  484. parse_client_method_error(line);
  485. goto err;
  486. } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
  487. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  488. goto err;
  489. parse_server_method_error(line);
  490. goto err;
  491. } else if (!strcmpstart(line, PROTO_CMETHOD)) {
  492. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  493. goto err;
  494. if (parse_cmethod_line(line, mp) < 0)
  495. goto err;
  496. return;
  497. } else if (!strcmpstart(line, PROTO_SMETHOD)) {
  498. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  499. goto err;
  500. if (parse_smethod_line(line, mp) < 0)
  501. goto err;
  502. return;
  503. } else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
  504. log_warn(LD_GENERAL, "Could not launch managed proxy executable!");
  505. goto err;
  506. }
  507. log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
  508. err:
  509. mp->conf_state = PT_PROTO_BROKEN;
  510. return;
  511. }
  512. /** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
  513. void
  514. parse_env_error(const char *line)
  515. {
  516. /* (Length of the protocol string) plus (a space) and (the first char of
  517. the error message) */
  518. if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
  519. log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
  520. "message.", PROTO_ENV_ERROR);
  521. log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
  522. "pluggable transport environment variables. (%s)",
  523. line+strlen(PROTO_ENV_ERROR)+1);
  524. }
  525. /** Handles a VERSION <b>line</b>. Updates the configuration protocol
  526. * version in <b>mp</b>. */
  527. int
  528. parse_version(const char *line, managed_proxy_t *mp)
  529. {
  530. if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
  531. log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
  532. PROTO_NEG_SUCCESS);
  533. return -1;
  534. }
  535. if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) { /* hardcoded temp */
  536. log_warn(LD_CONFIG, "Managed proxy tried to negotiate on version '%s'. "
  537. "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
  538. return -1;
  539. }
  540. mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
  541. return 0;
  542. }
  543. /** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
  544. * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
  545. * otherwise it is a CMETHOD-ERROR. */
  546. static void
  547. parse_method_error(const char *line, int is_server)
  548. {
  549. const char* error = is_server ?
  550. PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
  551. /* (Length of the protocol string) plus (a space) and (the first char of
  552. the error message) */
  553. if (strlen(line) < (strlen(error) + 2))
  554. log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
  555. "message.", error);
  556. log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
  557. is_server ? "Server" : "Client",
  558. line+strlen(error)+1);
  559. }
  560. /** Parses an SMETHOD <b>line</b> and if well-formed it registers the
  561. * new transport in <b>mp</b>. */
  562. int
  563. parse_smethod_line(const char *line, managed_proxy_t *mp)
  564. {
  565. int r;
  566. smartlist_t *items = NULL;
  567. char *method_name=NULL;
  568. char *addrport=NULL;
  569. tor_addr_t addr;
  570. uint16_t port = 0;
  571. transport_t *transport=NULL;
  572. items = smartlist_create();
  573. smartlist_split_string(items, line, NULL,
  574. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  575. if (smartlist_len(items) < 3) {
  576. log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
  577. "with too few arguments.");
  578. goto err;
  579. }
  580. tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
  581. method_name = smartlist_get(items,1);
  582. if (!string_is_C_identifier(method_name)) {
  583. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  584. method_name);
  585. goto err;
  586. }
  587. addrport = smartlist_get(items, 2);
  588. if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
  589. log_warn(LD_CONFIG, "Error parsing transport "
  590. "address '%s'", addrport);
  591. goto err;
  592. }
  593. if (!port) {
  594. log_warn(LD_CONFIG,
  595. "Transport address '%s' has no port.", addrport);
  596. goto err;
  597. }
  598. transport = transport_create(&addr, port, method_name, PROXY_NONE);
  599. if (!transport)
  600. goto err;
  601. smartlist_add(mp->transports, transport);
  602. /* For now, notify the user so that he knows where the server
  603. transport is listening. */
  604. log_info(LD_CONFIG, "Server transport %s at %s:%d.",
  605. method_name, fmt_addr(&addr), (int)port);
  606. r=0;
  607. goto done;
  608. err:
  609. r = -1;
  610. done:
  611. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  612. smartlist_free(items);
  613. return r;
  614. }
  615. /** Parses a CMETHOD <b>line</b>, and if well-formed it registers
  616. * the new transport in <b>mp</b>. */
  617. int
  618. parse_cmethod_line(const char *line, managed_proxy_t *mp)
  619. {
  620. int r;
  621. smartlist_t *items = NULL;
  622. char *method_name=NULL;
  623. char *socks_ver_str=NULL;
  624. int socks_ver=PROXY_NONE;
  625. char *addrport=NULL;
  626. tor_addr_t addr;
  627. uint16_t port = 0;
  628. transport_t *transport=NULL;
  629. items = smartlist_create();
  630. smartlist_split_string(items, line, NULL,
  631. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  632. if (smartlist_len(items) < 4) {
  633. log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
  634. "with too few arguments.");
  635. goto err;
  636. }
  637. tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
  638. method_name = smartlist_get(items,1);
  639. if (!string_is_C_identifier(method_name)) {
  640. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  641. method_name);
  642. goto err;
  643. }
  644. socks_ver_str = smartlist_get(items,2);
  645. if (!strcmp(socks_ver_str,"socks4")) {
  646. socks_ver = PROXY_SOCKS4;
  647. } else if (!strcmp(socks_ver_str,"socks5")) {
  648. socks_ver = PROXY_SOCKS5;
  649. } else {
  650. log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
  651. "we don't recognize. (%s)", socks_ver_str);
  652. goto err;
  653. }
  654. addrport = smartlist_get(items, 3);
  655. if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
  656. log_warn(LD_CONFIG, "Error parsing transport "
  657. "address '%s'", addrport);
  658. goto err;
  659. }
  660. if (!port) {
  661. log_warn(LD_CONFIG,
  662. "Transport address '%s' has no port.", addrport);
  663. goto err;
  664. }
  665. transport = transport_create(&addr, port, method_name, socks_ver);
  666. if (!transport)
  667. goto err;
  668. smartlist_add(mp->transports, transport);
  669. log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
  670. "Attached to managed proxy.",
  671. method_name, fmt_addr(&addr), (int)port, socks_ver);
  672. r=0;
  673. goto done;
  674. err:
  675. r = -1;
  676. done:
  677. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  678. smartlist_free(items);
  679. return r;
  680. }
  681. /** Return a string containing the address:port that <b>transport</b>
  682. * should use. It's the responsibility of the caller to free() the
  683. * received string. */
  684. static char *
  685. get_bindaddr_for_proxy(const managed_proxy_t *mp)
  686. {
  687. char *bindaddr = NULL;
  688. smartlist_t *string_tmp = smartlist_create();
  689. tor_assert(mp->is_server);
  690. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
  691. tor_asprintf(&bindaddr, "%s-%s", t, get_bindaddr_for_transport(t));
  692. smartlist_add(string_tmp, bindaddr);
  693. } SMARTLIST_FOREACH_END(t);
  694. bindaddr = smartlist_join_strings(string_tmp, ",", 0, NULL);
  695. SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  696. smartlist_free(string_tmp);
  697. return bindaddr;
  698. }
  699. /** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
  700. static void
  701. set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
  702. {
  703. const or_options_t *options = get_options();
  704. char **tmp=NULL;
  705. char *state_loc=NULL;
  706. char *transports_to_launch=NULL;
  707. char *bindaddr=NULL;
  708. int n_envs = mp->is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
  709. /* allocate enough space for our env. vars and a NULL pointer */
  710. *envp = tor_malloc(sizeof(char*)*(n_envs+1));
  711. tmp = *envp;
  712. state_loc = get_datadir_fname("pt_state/"); /* XXX temp */
  713. transports_to_launch =
  714. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  715. tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
  716. tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
  717. tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
  718. tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
  719. if (mp->is_server) {
  720. bindaddr = get_bindaddr_for_proxy(mp);
  721. /* XXX temp */
  722. tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort);
  723. tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
  724. tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
  725. /* XXX temp*/
  726. tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200");
  727. } else {
  728. tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
  729. }
  730. *tmp = NULL;
  731. tor_free(state_loc);
  732. tor_free(transports_to_launch);
  733. tor_free(bindaddr);
  734. }
  735. /** Create and return a new managed proxy for <b>transport</b> using
  736. * <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
  737. * managed proxy. */
  738. static managed_proxy_t *
  739. managed_proxy_create(const smartlist_t *transport_list,
  740. char **proxy_argv, int is_server)
  741. {
  742. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  743. mp->conf_state = PT_PROTO_INFANT;
  744. mp->is_server = is_server;
  745. mp->argv = proxy_argv;
  746. mp->transports = smartlist_create();
  747. mp->transports_to_launch = smartlist_create();
  748. SMARTLIST_FOREACH(transport_list, const char *, transport,
  749. add_transport_to_proxy(transport, mp));
  750. /* register the managed proxy */
  751. if (!managed_proxy_list)
  752. managed_proxy_list = smartlist_create();
  753. smartlist_add(managed_proxy_list, mp);
  754. unconfigured_proxies_n++;
  755. return mp;
  756. }
  757. /** Register <b>transport</b> using proxy with <b>proxy_argv</b> to
  758. * the managed proxy subsystem.
  759. * If <b>is_server</b> is true, then the proxy is a server proxy. */
  760. void
  761. pt_kickstart_proxy(const smartlist_t *transport_list,
  762. char **proxy_argv, int is_server)
  763. {
  764. managed_proxy_t *mp=NULL;
  765. transport_t *old_transport = NULL;
  766. mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
  767. if (!mp) { /* we haven't seen this proxy before */
  768. managed_proxy_create(transport_list, proxy_argv, is_server);
  769. } else { /* known proxy. add its transport to its transport list */
  770. if (mp->got_hup) {
  771. /* If the managed proxy we found is marked by a SIGHUP, it means
  772. that it's not useless and should be kept. If it's marked for
  773. removal, unmark it and increase the unconfigured proxies so
  774. that we try to restart it if we need to. Afterwards, check if
  775. a transport_t for 'transport' used to exist before the SIGHUP
  776. and make sure it doesn't get deleted because we might reuse
  777. it. */
  778. if (mp->marked_for_removal) {
  779. mp->marked_for_removal = 0;
  780. unconfigured_proxies_n++;
  781. }
  782. SMARTLIST_FOREACH_BEGIN(transport_list, const char *, transport) {
  783. old_transport = transport_get_by_name(transport);
  784. if (old_transport)
  785. old_transport->marked_for_removal = 0;
  786. } SMARTLIST_FOREACH_END(transport);
  787. }
  788. SMARTLIST_FOREACH(transport_list, const char *, transport,
  789. add_transport_to_proxy(transport, mp));
  790. free_execve_args(proxy_argv);
  791. }
  792. }
  793. /** Frees the array of pointers in <b>arg</b> used as arguments to
  794. execve(2). */
  795. static INLINE void
  796. free_execve_args(char **arg)
  797. {
  798. char **tmp = arg;
  799. while (*tmp) /* use the fact that the last element of the array is a
  800. NULL pointer to know when to stop freeing */
  801. _tor_free(*tmp++);
  802. tor_free(arg);
  803. }
  804. /** Tor will read its config.
  805. * Prepare the managed proxy list so that proxies not used in the new
  806. * config will shutdown, and proxies that need to spawn different
  807. * transports will do so. */
  808. void
  809. pt_prepare_proxy_list_for_config_read(void)
  810. {
  811. if (!managed_proxy_list)
  812. return;
  813. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  814. /* Destroy unconfigured proxies. */
  815. if (mp->conf_state != PT_PROTO_COMPLETED) {
  816. managed_proxy_destroy(mp);
  817. unconfigured_proxies_n--;
  818. continue;
  819. }
  820. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  821. mp->marked_for_removal = 1;
  822. mp->got_hup = 1;
  823. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  824. smartlist_clear(mp->transports_to_launch);
  825. } SMARTLIST_FOREACH_END(mp);
  826. tor_assert(unconfigured_proxies_n == 0);
  827. }
  828. /** The tor config was read.
  829. * Destroy all managed proxies that were marked by a previous call to
  830. * prepare_proxy_list_for_config_read() and are not used by the new
  831. * config. */
  832. void
  833. sweep_proxy_list(void)
  834. {
  835. if (!managed_proxy_list)
  836. return;
  837. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  838. if (mp->marked_for_removal) {
  839. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  840. managed_proxy_destroy(mp);
  841. }
  842. } SMARTLIST_FOREACH_END(mp);
  843. }
  844. /** Release all storage held by the pluggable transports subsystem. */
  845. void
  846. pt_free_all(void)
  847. {
  848. if (managed_proxy_list) {
  849. /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
  850. transports and it's the duty of the circuitbuild.c subsystem to
  851. free them. Otherwise, it hasn't registered its transports yet
  852. and we should free them here. */
  853. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp,
  854. managed_proxy_destroy(mp));
  855. smartlist_free(managed_proxy_list);
  856. managed_proxy_list=NULL;
  857. }
  858. }