transports.c 45 KB

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