transports.c 45 KB

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