transports.c 48 KB

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