transports.c 43 KB

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