transports.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /* Copyright (c) 2011-2018, 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 config read
  54. * (like a SIGHUP or a SETCONF) occurs:</b>
  55. *
  56. * We immediately destroy all unconfigured proxies (We shouldn't have
  57. * unconfigured proxies in the first place, except when the config
  58. * read happens 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
  66. * (we mark using the <b>was_around_before_config_read</b> element).
  67. * We also clear their <b>transports_to_launch</b> list so that we can
  68. * put there the transports we need to launch according to the new
  69. * torrc.
  70. *
  71. * We then start parsing torrc again.
  72. *
  73. * Everytime we encounter a transport line using a managed proxy that
  74. * was around before the config read, we cleanse that proxy from the
  75. * removal mark. We also toggle the <b>check_if_restarts_needed</b>
  76. * flag, so that on the next <b>pt_configure_remaining_proxies</b>
  77. * tick, we investigate whether we need to restart the proxy so that
  78. * it also spawns the new transports. If the post-config-read
  79. * <b>transports_to_launch</b> list is identical to the pre-config-read
  80. * one, it means that no changes were introduced to this proxy during
  81. * the config read and no restart has to take place.
  82. *
  83. * During the post-config-read torrc parsing, we unmark all transports
  84. * spawned by managed proxies that we find in our torrc.
  85. * We do that so that if we don't need to restart a managed proxy, we
  86. * can continue using its old transports normally.
  87. * If we end up restarting the proxy, we destroy and unregister all
  88. * old transports from the circuitbuild.c subsystem.
  89. **/
  90. #define PT_PRIVATE
  91. #include "core/or/or.h"
  92. #include "feature/client/bridges.h"
  93. #include "app/config/config.h"
  94. #include "core/mainloop/connection.h"
  95. #include "core/or/circuitbuild.h"
  96. #include "feature/client/transports.h"
  97. #include "feature/relay/router.h"
  98. #include "app/config/statefile.h"
  99. #include "core/or/connection_or.h"
  100. #include "feature/relay/ext_orport.h"
  101. #include "feature/control/control.h"
  102. #include "lib/process/process.h"
  103. #include "lib/process/env.h"
  104. static smartlist_t *
  105. create_managed_proxy_environment(const managed_proxy_t *mp);
  106. static inline int proxy_configuration_finished(const managed_proxy_t *mp);
  107. static void handle_finished_proxy(managed_proxy_t *mp);
  108. static void parse_method_error(const char *line, int is_server_method);
  109. #define parse_server_method_error(l) parse_method_error(l, 1)
  110. #define parse_client_method_error(l) parse_method_error(l, 0)
  111. /** Managed proxy protocol strings */
  112. #define PROTO_ENV_ERROR "ENV-ERROR"
  113. #define PROTO_NEG_SUCCESS "VERSION"
  114. #define PROTO_NEG_FAIL "VERSION-ERROR no-version"
  115. #define PROTO_CMETHOD "CMETHOD"
  116. #define PROTO_SMETHOD "SMETHOD"
  117. #define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
  118. #define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
  119. #define PROTO_CMETHODS_DONE "CMETHODS DONE"
  120. #define PROTO_SMETHODS_DONE "SMETHODS DONE"
  121. #define PROTO_PROXY_DONE "PROXY DONE"
  122. #define PROTO_PROXY_ERROR "PROXY-ERROR"
  123. #define PROTO_LOG "LOG"
  124. /** The first and only supported - at the moment - configuration
  125. protocol version. */
  126. #define PROTO_VERSION_ONE 1
  127. /** A list of pluggable transports found in torrc. */
  128. static smartlist_t *transport_list = NULL;
  129. /** Returns a transport_t struct for a transport proxy supporting the
  130. protocol <b>name</b> listening at <b>addr</b>:<b>port</b> using
  131. SOCKS version <b>socks_ver</b>. */
  132. STATIC transport_t *
  133. transport_new(const tor_addr_t *addr, uint16_t port,
  134. const char *name, int socks_ver,
  135. const char *extra_info_args)
  136. {
  137. transport_t *t = tor_malloc_zero(sizeof(transport_t));
  138. tor_addr_copy(&t->addr, addr);
  139. t->port = port;
  140. t->name = tor_strdup(name);
  141. t->socks_version = socks_ver;
  142. if (extra_info_args)
  143. t->extra_info_args = tor_strdup(extra_info_args);
  144. return t;
  145. }
  146. /** Free the pluggable transport struct <b>transport</b>. */
  147. void
  148. transport_free_(transport_t *transport)
  149. {
  150. if (!transport)
  151. return;
  152. tor_free(transport->name);
  153. tor_free(transport->extra_info_args);
  154. tor_free(transport);
  155. }
  156. /** Mark every entry of the transport list to be removed on our next call to
  157. * sweep_transport_list unless it has first been un-marked. */
  158. void
  159. mark_transport_list(void)
  160. {
  161. if (!transport_list)
  162. transport_list = smartlist_new();
  163. SMARTLIST_FOREACH(transport_list, transport_t *, t,
  164. t->marked_for_removal = 1);
  165. }
  166. /** Remove every entry of the transport list that was marked with
  167. * mark_transport_list if it has not subsequently been un-marked. */
  168. void
  169. sweep_transport_list(void)
  170. {
  171. if (!transport_list)
  172. transport_list = smartlist_new();
  173. SMARTLIST_FOREACH_BEGIN(transport_list, transport_t *, t) {
  174. if (t->marked_for_removal) {
  175. SMARTLIST_DEL_CURRENT(transport_list, t);
  176. transport_free(t);
  177. }
  178. } SMARTLIST_FOREACH_END(t);
  179. }
  180. /** Initialize the pluggable transports list to empty, creating it if
  181. * needed. */
  182. static void
  183. clear_transport_list(void)
  184. {
  185. if (!transport_list)
  186. transport_list = smartlist_new();
  187. SMARTLIST_FOREACH(transport_list, transport_t *, t, transport_free(t));
  188. smartlist_clear(transport_list);
  189. }
  190. /** Return a deep copy of <b>transport</b>. */
  191. static transport_t *
  192. transport_copy(const transport_t *transport)
  193. {
  194. transport_t *new_transport = NULL;
  195. tor_assert(transport);
  196. new_transport = tor_malloc_zero(sizeof(transport_t));
  197. new_transport->socks_version = transport->socks_version;
  198. new_transport->name = tor_strdup(transport->name);
  199. tor_addr_copy(&new_transport->addr, &transport->addr);
  200. new_transport->port = transport->port;
  201. new_transport->marked_for_removal = transport->marked_for_removal;
  202. return new_transport;
  203. }
  204. /** Returns the transport in our transport list that has the name <b>name</b>.
  205. * Else returns NULL. */
  206. MOCK_IMPL(transport_t *,
  207. transport_get_by_name,(const char *name))
  208. {
  209. tor_assert(name);
  210. if (!transport_list)
  211. return NULL;
  212. SMARTLIST_FOREACH_BEGIN(transport_list, transport_t *, transport) {
  213. if (!strcmp(transport->name, name))
  214. return transport;
  215. } SMARTLIST_FOREACH_END(transport);
  216. return NULL;
  217. }
  218. /** Resolve any conflicts that the insertion of transport <b>t</b>
  219. * might cause.
  220. * Return 0 if <b>t</b> is OK and should be registered, 1 if there is
  221. * a transport identical to <b>t</b> already registered and -1 if
  222. * <b>t</b> cannot be added due to conflicts. */
  223. static int
  224. transport_resolve_conflicts(const transport_t *t)
  225. {
  226. /* This is how we resolve transport conflicts:
  227. If there is already a transport with the same name and addrport,
  228. we either have duplicate torrc lines OR we are here post-HUP and
  229. this transport was here pre-HUP as well. In any case, mark the
  230. old transport so that it doesn't get removed and ignore the new
  231. one. Our caller has to free the new transport so we return '1' to
  232. signify this.
  233. If there is already a transport with the same name but different
  234. addrport:
  235. * if it's marked for removal, it means that it either has a lower
  236. priority than 't' in torrc (otherwise the mark would have been
  237. cleared by the paragraph above), or it doesn't exist at all in
  238. the post-HUP torrc. We destroy the old transport and register 't'.
  239. * if it's *not* marked for removal, it means that it was newly
  240. added in the post-HUP torrc or that it's of higher priority, in
  241. this case we ignore 't'. */
  242. transport_t *t_tmp = transport_get_by_name(t->name);
  243. if (t_tmp) { /* same name */
  244. if (tor_addr_eq(&t->addr, &t_tmp->addr) && (t->port == t_tmp->port)) {
  245. /* same name *and* addrport */
  246. t_tmp->marked_for_removal = 0;
  247. return 1;
  248. } else { /* same name but different addrport */
  249. char *new_transport_addrport =
  250. tor_strdup(fmt_addrport(&t->addr, t->port));
  251. if (t_tmp->marked_for_removal) { /* marked for removal */
  252. log_notice(LD_GENERAL, "You tried to add transport '%s' at '%s' "
  253. "but there was already a transport marked for deletion at "
  254. "'%s'. We deleted the old transport and registered the "
  255. "new one.", t->name, new_transport_addrport,
  256. fmt_addrport(&t_tmp->addr, t_tmp->port));
  257. smartlist_remove(transport_list, t_tmp);
  258. transport_free(t_tmp);
  259. tor_free(new_transport_addrport);
  260. } else { /* *not* marked for removal */
  261. log_notice(LD_GENERAL, "You tried to add transport '%s' at '%s' "
  262. "but the same transport already exists at '%s'. "
  263. "Skipping.", t->name, new_transport_addrport,
  264. fmt_addrport(&t_tmp->addr, t_tmp->port));
  265. tor_free(new_transport_addrport);
  266. return -1;
  267. }
  268. tor_free(new_transport_addrport);
  269. }
  270. }
  271. return 0;
  272. }
  273. /** Add transport <b>t</b> to the internal list of pluggable
  274. * transports.
  275. * Returns 0 if the transport was added correctly, 1 if the same
  276. * transport was already registered (in this case the caller must
  277. * free the transport) and -1 if there was an error. */
  278. static int
  279. transport_add(transport_t *t)
  280. {
  281. int r;
  282. tor_assert(t);
  283. r = transport_resolve_conflicts(t);
  284. switch (r) {
  285. case 0: /* should register transport */
  286. if (!transport_list)
  287. transport_list = smartlist_new();
  288. smartlist_add(transport_list, t);
  289. return 0;
  290. default: /* let our caller know the return code */
  291. return r;
  292. }
  293. }
  294. /** Remember a new pluggable transport proxy at <b>addr</b>:<b>port</b>.
  295. * <b>name</b> is set to the name of the protocol this proxy uses.
  296. * <b>socks_ver</b> is set to the SOCKS version of the proxy. */
  297. MOCK_IMPL(int,
  298. transport_add_from_config, (const tor_addr_t *addr, uint16_t port,
  299. const char *name, int socks_ver))
  300. {
  301. transport_t *t = transport_new(addr, port, name, socks_ver, NULL);
  302. int r = transport_add(t);
  303. switch (r) {
  304. case -1:
  305. default:
  306. log_notice(LD_GENERAL, "Could not add transport %s at %s. Skipping.",
  307. t->name, fmt_addrport(&t->addr, t->port));
  308. transport_free(t);
  309. return -1;
  310. case 1:
  311. log_info(LD_GENERAL, "Successfully registered transport %s at %s.",
  312. t->name, fmt_addrport(&t->addr, t->port));
  313. transport_free(t); /* falling */
  314. return 0;
  315. case 0:
  316. log_info(LD_GENERAL, "Successfully registered transport %s at %s.",
  317. t->name, fmt_addrport(&t->addr, t->port));
  318. return 0;
  319. }
  320. }
  321. /** List of unconfigured managed proxies. */
  322. static smartlist_t *managed_proxy_list = NULL;
  323. /** Number of still unconfigured proxies. */
  324. static int unconfigured_proxies_n = 0;
  325. /** Boolean: True iff we might need to restart some proxies. */
  326. static int check_if_restarts_needed = 0;
  327. /** Return true if there are still unconfigured managed proxies, or proxies
  328. * that need restarting. */
  329. int
  330. pt_proxies_configuration_pending(void)
  331. {
  332. return unconfigured_proxies_n || check_if_restarts_needed;
  333. }
  334. /** Assert that the unconfigured_proxies_n value correctly matches the number
  335. * of proxies in a state other than PT_PROTO_COMPLETE. */
  336. static void
  337. assert_unconfigured_count_ok(void)
  338. {
  339. int n_completed = 0;
  340. if (!managed_proxy_list) {
  341. tor_assert(unconfigured_proxies_n == 0);
  342. return;
  343. }
  344. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
  345. if (mp->conf_state == PT_PROTO_COMPLETED)
  346. ++n_completed;
  347. });
  348. tor_assert(n_completed + unconfigured_proxies_n ==
  349. smartlist_len(managed_proxy_list));
  350. }
  351. /** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
  352. static int
  353. managed_proxy_has_argv(const managed_proxy_t *mp, char **proxy_argv)
  354. {
  355. char **tmp1=proxy_argv;
  356. char **tmp2=mp->argv;
  357. tor_assert(tmp1);
  358. tor_assert(tmp2);
  359. while (*tmp1 && *tmp2) {
  360. if (strcmp(*tmp1++, *tmp2++))
  361. return 0;
  362. }
  363. if (!*tmp1 && !*tmp2)
  364. return 1;
  365. return 0;
  366. }
  367. /** Return a managed proxy with the same argv as <b>proxy_argv</b>.
  368. * If no such managed proxy exists, return NULL. */
  369. static managed_proxy_t *
  370. get_managed_proxy_by_argv_and_type(char **proxy_argv, int is_server)
  371. {
  372. if (!managed_proxy_list)
  373. return NULL;
  374. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  375. if (managed_proxy_has_argv(mp, proxy_argv) &&
  376. mp->is_server == is_server)
  377. return mp;
  378. } SMARTLIST_FOREACH_END(mp);
  379. return NULL;
  380. }
  381. /** Add <b>transport</b> to managed proxy <b>mp</b>. */
  382. static void
  383. add_transport_to_proxy(const char *transport, managed_proxy_t *mp)
  384. {
  385. tor_assert(mp->transports_to_launch);
  386. if (!smartlist_contains_string(mp->transports_to_launch, transport))
  387. smartlist_add_strdup(mp->transports_to_launch, transport);
  388. }
  389. /** Called when a SIGHUP occurs. Returns true if managed proxy
  390. * <b>mp</b> needs to be restarted after the SIGHUP, based on the new
  391. * torrc. */
  392. static int
  393. proxy_needs_restart(const managed_proxy_t *mp)
  394. {
  395. int ret = 1;
  396. char* proxy_uri;
  397. /* If the PT proxy config has changed, then all existing pluggable transports
  398. * should be restarted.
  399. */
  400. proxy_uri = get_pt_proxy_uri();
  401. if (strcmp_opt(proxy_uri, mp->proxy_uri) != 0)
  402. goto needs_restart;
  403. /* mp->transport_to_launch is populated with the names of the
  404. transports that must be launched *after* the SIGHUP.
  405. mp->transports is populated with the transports that were
  406. launched *before* the SIGHUP.
  407. Check if all the transports that need to be launched are already
  408. launched: */
  409. tor_assert(smartlist_len(mp->transports_to_launch) > 0);
  410. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  411. if (smartlist_len(mp->transports_to_launch) != smartlist_len(mp->transports))
  412. goto needs_restart;
  413. SMARTLIST_FOREACH_BEGIN(mp->transports, const transport_t *, t) {
  414. if (!smartlist_contains_string(mp->transports_to_launch, t->name))
  415. goto needs_restart;
  416. } SMARTLIST_FOREACH_END(t);
  417. ret = 0;
  418. needs_restart:
  419. tor_free(proxy_uri);
  420. return ret;
  421. }
  422. /** Managed proxy <b>mp</b> must be restarted. Do all the necessary
  423. * preparations and then flag its state so that it will be relaunched
  424. * in the next tick. */
  425. static void
  426. proxy_prepare_for_restart(managed_proxy_t *mp)
  427. {
  428. transport_t *t_tmp = NULL;
  429. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  430. /* destroy the process handle and terminate the process. */
  431. process_set_data(mp->process, NULL);
  432. process_terminate(mp->process);
  433. /* destroy all its registered transports, since we will no longer
  434. use them. */
  435. SMARTLIST_FOREACH_BEGIN(mp->transports, const transport_t *, t) {
  436. t_tmp = transport_get_by_name(t->name);
  437. if (t_tmp)
  438. t_tmp->marked_for_removal = 1;
  439. } SMARTLIST_FOREACH_END(t);
  440. sweep_transport_list();
  441. /* free the transport in mp->transports */
  442. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  443. smartlist_clear(mp->transports);
  444. /* Reset the proxy's HTTPS/SOCKS proxy */
  445. tor_free(mp->proxy_uri);
  446. mp->proxy_uri = get_pt_proxy_uri();
  447. mp->proxy_supported = 0;
  448. /* flag it as an infant proxy so that it gets launched on next tick */
  449. mp->conf_state = PT_PROTO_INFANT;
  450. unconfigured_proxies_n++;
  451. }
  452. /** Launch managed proxy <b>mp</b>. */
  453. static int
  454. launch_managed_proxy(managed_proxy_t *mp)
  455. {
  456. tor_assert(mp);
  457. smartlist_t *env = create_managed_proxy_environment(mp);
  458. /* Configure our process. */
  459. process_set_data(mp->process, mp);
  460. process_set_stdout_read_callback(mp->process, managed_proxy_stdout_callback);
  461. process_set_stderr_read_callback(mp->process, managed_proxy_stderr_callback);
  462. process_set_exit_callback(mp->process, managed_proxy_exit_callback);
  463. process_set_protocol(mp->process, PROCESS_PROTOCOL_LINE);
  464. process_reset_environment(mp->process, env);
  465. /* Cleanup our env. */
  466. SMARTLIST_FOREACH(env, char *, x, tor_free(x));
  467. smartlist_free(env);
  468. /* Skip the argv[0] as we get that from process_new(argv[0]). */
  469. for (int i = 1; mp->argv[i] != NULL; ++i)
  470. process_append_argument(mp->process, mp->argv[i]);
  471. if (process_exec(mp->process) != PROCESS_STATUS_RUNNING) {
  472. log_warn(LD_CONFIG, "Managed proxy at '%s' failed at launch.",
  473. mp->argv[0]);
  474. return -1;
  475. }
  476. log_info(LD_CONFIG,
  477. "Managed proxy at '%s' has spawned with PID '%" PRIu64 "'.",
  478. mp->argv[0], process_get_pid(mp->process));
  479. mp->conf_state = PT_PROTO_LAUNCHED;
  480. return 0;
  481. }
  482. /** Check if any of the managed proxies we are currently trying to
  483. * configure has anything new to say. */
  484. void
  485. pt_configure_remaining_proxies(void)
  486. {
  487. int at_least_a_proxy_config_finished = 0;
  488. smartlist_t *tmp = smartlist_new();
  489. log_debug(LD_CONFIG, "Configuring remaining managed proxies (%d)!",
  490. unconfigured_proxies_n);
  491. /* Iterate over tmp, not managed_proxy_list, since configure_proxy can
  492. * remove elements from managed_proxy_list. */
  493. smartlist_add_all(tmp, managed_proxy_list);
  494. assert_unconfigured_count_ok();
  495. SMARTLIST_FOREACH_BEGIN(tmp, managed_proxy_t *, mp) {
  496. tor_assert(mp->conf_state != PT_PROTO_BROKEN &&
  497. mp->conf_state != PT_PROTO_FAILED_LAUNCH);
  498. if (mp->was_around_before_config_read) {
  499. /* This proxy is marked by a config read. Check whether we need
  500. to restart it. */
  501. mp->was_around_before_config_read = 0;
  502. if (proxy_needs_restart(mp)) {
  503. log_info(LD_GENERAL, "Preparing managed proxy '%s' for restart.",
  504. mp->argv[0]);
  505. proxy_prepare_for_restart(mp);
  506. } else { /* it doesn't need to be restarted. */
  507. log_info(LD_GENERAL, "Nothing changed for managed proxy '%s' after "
  508. "HUP: not restarting.", mp->argv[0]);
  509. }
  510. continue;
  511. }
  512. /* If the proxy is not fully configured, try to configure it
  513. further. */
  514. if (!proxy_configuration_finished(mp))
  515. if (configure_proxy(mp) == 1)
  516. at_least_a_proxy_config_finished = 1;
  517. } SMARTLIST_FOREACH_END(mp);
  518. smartlist_free(tmp);
  519. check_if_restarts_needed = 0;
  520. assert_unconfigured_count_ok();
  521. if (at_least_a_proxy_config_finished)
  522. mark_my_descriptor_dirty("configured managed proxies");
  523. }
  524. /** Attempt to continue configuring managed proxy <b>mp</b>.
  525. * Return 1 if the transport configuration finished, and return 0
  526. * otherwise (if we still have more configuring to do for this
  527. * proxy). */
  528. STATIC int
  529. configure_proxy(managed_proxy_t *mp)
  530. {
  531. /* if we haven't launched the proxy yet, do it now */
  532. if (mp->conf_state == PT_PROTO_INFANT) {
  533. if (launch_managed_proxy(mp) < 0) { /* launch fail */
  534. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  535. handle_finished_proxy(mp);
  536. }
  537. return 0;
  538. }
  539. tor_assert(mp->conf_state != PT_PROTO_INFANT);
  540. tor_assert(mp->process);
  541. return mp->conf_state == PT_PROTO_COMPLETED;
  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. control_event_transport_launched("server", t->name, &t->addr, t->port);
  553. } SMARTLIST_FOREACH_END(t);
  554. }
  555. /** Register all the transports supported by client managed proxy
  556. * <b>mp</b> to the bridge subsystem. */
  557. static void
  558. register_client_proxy(const managed_proxy_t *mp)
  559. {
  560. int r;
  561. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  562. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  563. transport_t *transport_tmp = transport_copy(t);
  564. r = transport_add(transport_tmp);
  565. switch (r) {
  566. case -1:
  567. log_notice(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
  568. transport_free(transport_tmp);
  569. break;
  570. case 0:
  571. log_info(LD_GENERAL, "Successfully registered transport %s", t->name);
  572. control_event_transport_launched("client", t->name, &t->addr, t->port);
  573. break;
  574. case 1:
  575. log_info(LD_GENERAL, "Successfully registered transport %s", t->name);
  576. control_event_transport_launched("client", t->name, &t->addr, t->port);
  577. transport_free(transport_tmp);
  578. break;
  579. }
  580. } SMARTLIST_FOREACH_END(t);
  581. }
  582. /** Register the transports of managed proxy <b>mp</b>. */
  583. static inline void
  584. register_proxy(const managed_proxy_t *mp)
  585. {
  586. if (mp->is_server)
  587. register_server_proxy(mp);
  588. else
  589. register_client_proxy(mp);
  590. }
  591. /** Free memory allocated by managed proxy <b>mp</b>. */
  592. STATIC void
  593. managed_proxy_destroy(managed_proxy_t *mp,
  594. int also_terminate_process)
  595. {
  596. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  597. /* free the transports smartlist */
  598. smartlist_free(mp->transports);
  599. /* free the transports_to_launch smartlist */
  600. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  601. smartlist_free(mp->transports_to_launch);
  602. /* remove it from the list of managed proxies */
  603. if (managed_proxy_list)
  604. smartlist_remove(managed_proxy_list, mp);
  605. /* free the argv */
  606. free_execve_args(mp->argv);
  607. /* free the outgoing proxy URI */
  608. tor_free(mp->proxy_uri);
  609. /* do we want to terminate our process if it's still running? */
  610. if (also_terminate_process && mp->process)
  611. process_terminate(mp->process);
  612. process_free(mp->process);
  613. tor_free(mp);
  614. }
  615. /** Convert the tor proxy options to a URI suitable for TOR_PT_PROXY.
  616. * Return a newly allocated string containing the URI, or NULL if no
  617. * proxy is set. */
  618. STATIC char *
  619. get_pt_proxy_uri(void)
  620. {
  621. const or_options_t *options = get_options();
  622. char *uri = NULL;
  623. if (options->Socks4Proxy || options->Socks5Proxy || options->HTTPSProxy) {
  624. char addr[TOR_ADDR_BUF_LEN+1];
  625. if (options->Socks4Proxy) {
  626. tor_addr_to_str(addr, &options->Socks4ProxyAddr, sizeof(addr), 1);
  627. tor_asprintf(&uri, "socks4a://%s:%d", addr, options->Socks4ProxyPort);
  628. } else if (options->Socks5Proxy) {
  629. tor_addr_to_str(addr, &options->Socks5ProxyAddr, sizeof(addr), 1);
  630. if (!options->Socks5ProxyUsername && !options->Socks5ProxyPassword) {
  631. tor_asprintf(&uri, "socks5://%s:%d", addr, options->Socks5ProxyPort);
  632. } else {
  633. tor_asprintf(&uri, "socks5://%s:%s@%s:%d",
  634. options->Socks5ProxyUsername,
  635. options->Socks5ProxyPassword,
  636. addr, options->Socks5ProxyPort);
  637. }
  638. } else if (options->HTTPSProxy) {
  639. tor_addr_to_str(addr, &options->HTTPSProxyAddr, sizeof(addr), 1);
  640. if (!options->HTTPSProxyAuthenticator) {
  641. tor_asprintf(&uri, "http://%s:%d", addr, options->HTTPSProxyPort);
  642. } else {
  643. tor_asprintf(&uri, "http://%s@%s:%d", options->HTTPSProxyAuthenticator,
  644. addr, options->HTTPSProxyPort);
  645. }
  646. }
  647. }
  648. return uri;
  649. }
  650. /** Handle a configured or broken managed proxy <b>mp</b>. */
  651. static void
  652. handle_finished_proxy(managed_proxy_t *mp)
  653. {
  654. switch (mp->conf_state) {
  655. case PT_PROTO_BROKEN: /* if broken: */
  656. managed_proxy_destroy(mp, 1); /* annihilate it. */
  657. break;
  658. case PT_PROTO_FAILED_LAUNCH: /* if it failed before launching: */
  659. managed_proxy_destroy(mp, 0); /* destroy it but don't terminate */
  660. break;
  661. case PT_PROTO_CONFIGURED: /* if configured correctly: */
  662. if (mp->proxy_uri && !mp->proxy_supported) {
  663. log_warn(LD_CONFIG, "Managed proxy '%s' did not configure the "
  664. "specified outgoing proxy and will be terminated.",
  665. mp->argv[0]);
  666. managed_proxy_destroy(mp, 1); /* annihilate it. */
  667. break;
  668. }
  669. register_proxy(mp); /* register its transports */
  670. mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
  671. break;
  672. case PT_PROTO_INFANT:
  673. case PT_PROTO_LAUNCHED:
  674. case PT_PROTO_ACCEPTING_METHODS:
  675. case PT_PROTO_COMPLETED:
  676. default:
  677. log_warn(LD_CONFIG, "Unexpected state '%d' of managed proxy '%s'.",
  678. (int)mp->conf_state, mp->argv[0]);
  679. tor_assert(0);
  680. }
  681. unconfigured_proxies_n--;
  682. }
  683. /** Return true if the configuration of the managed proxy <b>mp</b> is
  684. finished. */
  685. static inline int
  686. proxy_configuration_finished(const managed_proxy_t *mp)
  687. {
  688. return (mp->conf_state == PT_PROTO_CONFIGURED ||
  689. mp->conf_state == PT_PROTO_BROKEN ||
  690. mp->conf_state == PT_PROTO_FAILED_LAUNCH);
  691. }
  692. /** This function is called when a proxy sends an {S,C}METHODS DONE message. */
  693. static void
  694. handle_methods_done(const managed_proxy_t *mp)
  695. {
  696. tor_assert(mp->transports);
  697. if (smartlist_len(mp->transports) == 0)
  698. log_notice(LD_GENERAL, "Managed proxy '%s' was spawned successfully, "
  699. "but it didn't launch any pluggable transport listeners!",
  700. mp->argv[0]);
  701. log_info(LD_CONFIG, "%s managed proxy '%s' configuration completed!",
  702. mp->is_server ? "Server" : "Client",
  703. mp->argv[0]);
  704. }
  705. /** Handle a configuration protocol <b>line</b> received from a
  706. * managed proxy <b>mp</b>. */
  707. STATIC void
  708. handle_proxy_line(const char *line, managed_proxy_t *mp)
  709. {
  710. log_info(LD_GENERAL, "Got a line from managed proxy '%s': (%s)",
  711. mp->argv[0], line);
  712. if (!strcmpstart(line, PROTO_ENV_ERROR)) {
  713. if (mp->conf_state != PT_PROTO_LAUNCHED)
  714. goto err;
  715. parse_env_error(line);
  716. goto err;
  717. } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
  718. if (mp->conf_state != PT_PROTO_LAUNCHED)
  719. goto err;
  720. log_warn(LD_CONFIG, "Managed proxy could not pick a "
  721. "configuration protocol version.");
  722. goto err;
  723. } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
  724. if (mp->conf_state != PT_PROTO_LAUNCHED)
  725. goto err;
  726. if (parse_version(line,mp) < 0)
  727. goto err;
  728. tor_assert(mp->conf_protocol != 0);
  729. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  730. return;
  731. } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
  732. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  733. goto err;
  734. handle_methods_done(mp);
  735. mp->conf_state = PT_PROTO_CONFIGURED;
  736. return;
  737. } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
  738. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  739. goto err;
  740. handle_methods_done(mp);
  741. mp->conf_state = PT_PROTO_CONFIGURED;
  742. return;
  743. } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
  744. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  745. goto err;
  746. parse_client_method_error(line);
  747. goto err;
  748. } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
  749. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  750. goto err;
  751. parse_server_method_error(line);
  752. goto err;
  753. } else if (!strcmpstart(line, PROTO_CMETHOD)) {
  754. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  755. goto err;
  756. if (parse_cmethod_line(line, mp) < 0)
  757. goto err;
  758. return;
  759. } else if (!strcmpstart(line, PROTO_SMETHOD)) {
  760. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  761. goto err;
  762. if (parse_smethod_line(line, mp) < 0)
  763. goto err;
  764. return;
  765. } else if (!strcmpstart(line, PROTO_PROXY_DONE)) {
  766. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  767. goto err;
  768. if (mp->proxy_uri) {
  769. mp->proxy_supported = 1;
  770. return;
  771. }
  772. /* No proxy was configured, this should log */
  773. } else if (!strcmpstart(line, PROTO_PROXY_ERROR)) {
  774. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  775. goto err;
  776. parse_proxy_error(line);
  777. goto err;
  778. /* We check for the additional " " after the PROTO_LOG string to make sure
  779. * we can later extend this big if/else-if table with something that begins
  780. * with "LOG" without having to get the order right. */
  781. } else if (!strcmpstart(line, PROTO_LOG " ")) {
  782. parse_log_line(line, mp);
  783. return;
  784. }
  785. log_notice(LD_GENERAL, "Unknown line received by managed proxy (%s).", line);
  786. return;
  787. err:
  788. mp->conf_state = PT_PROTO_BROKEN;
  789. log_warn(LD_CONFIG, "Managed proxy at '%s' failed the configuration protocol"
  790. " and will be destroyed.", mp->argv[0]);
  791. }
  792. /** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
  793. STATIC void
  794. parse_env_error(const char *line)
  795. {
  796. /* (Length of the protocol string) plus (a space) and (the first char of
  797. the error message) */
  798. if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
  799. log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
  800. "message.", PROTO_ENV_ERROR);
  801. log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
  802. "pluggable transport environment variables. (%s)",
  803. line+strlen(PROTO_ENV_ERROR)+1);
  804. }
  805. /** Handles a VERSION <b>line</b>. Updates the configuration protocol
  806. * version in <b>mp</b>. */
  807. STATIC int
  808. parse_version(const char *line, managed_proxy_t *mp)
  809. {
  810. if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
  811. log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
  812. PROTO_NEG_SUCCESS);
  813. return -1;
  814. }
  815. if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) { /* hardcoded temp */
  816. log_warn(LD_CONFIG, "Managed proxy tried to negotiate on version '%s'. "
  817. "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
  818. return -1;
  819. }
  820. mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
  821. return 0;
  822. }
  823. /** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
  824. * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
  825. * otherwise it is a CMETHOD-ERROR. */
  826. static void
  827. parse_method_error(const char *line, int is_server)
  828. {
  829. const char* error = is_server ?
  830. PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
  831. /* (Length of the protocol string) plus (a space) and (the first char of
  832. the error message) */
  833. if (strlen(line) < (strlen(error) + 2))
  834. log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
  835. "message.", error);
  836. log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
  837. is_server ? "Server" : "Client",
  838. line+strlen(error)+1);
  839. }
  840. /** A helper for parse_{c,s}method_line(), bootstraps its
  841. * functionalities. If <b>is_smethod</b> is true then the
  842. * the line to parse is a SMETHOD line otherwise it is a
  843. * CMETHOD line*/
  844. static int
  845. parse_method_line_helper(const char *line,
  846. managed_proxy_t *mp,
  847. int is_smethod)
  848. {
  849. int item_index = 0;
  850. int r;
  851. char *transport_name=NULL;
  852. char *args_string=NULL;
  853. char *addrport=NULL;
  854. int socks_ver=PROXY_NONE;
  855. char *address=NULL;
  856. uint16_t port = 0;
  857. const char *method_str = is_smethod ? PROTO_SMETHOD : PROTO_CMETHOD;
  858. const int min_args_count = is_smethod ? 3 : 4;
  859. tor_addr_t tor_addr;
  860. transport_t *transport=NULL;
  861. smartlist_t *items= smartlist_new();
  862. smartlist_split_string(items, line, NULL,
  863. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  864. if (smartlist_len(items) < min_args_count) {
  865. log_warn(LD_CONFIG, "Managed proxy sent us a %s line "
  866. "with too few arguments.", method_str);
  867. goto err;
  868. }
  869. tor_assert(!strcmp(smartlist_get(items, item_index),method_str));
  870. ++item_index;
  871. transport_name = smartlist_get(items,item_index);
  872. ++item_index;
  873. if (!string_is_C_identifier(transport_name)) {
  874. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  875. transport_name);
  876. goto err;
  877. }
  878. /** Check for the proxy method sent to us in CMETHOD line. */
  879. if (!is_smethod) {
  880. const char *socks_ver_str = smartlist_get(items,item_index);
  881. ++item_index;
  882. if (!strcmp(socks_ver_str,"socks4")) {
  883. socks_ver = PROXY_SOCKS4;
  884. } else if (!strcmp(socks_ver_str,"socks5")) {
  885. socks_ver = PROXY_SOCKS5;
  886. } else {
  887. log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
  888. "we don't recognize. (%s)", socks_ver_str);
  889. goto err;
  890. }
  891. }
  892. addrport = smartlist_get(items, item_index);
  893. ++item_index;
  894. if (tor_addr_port_split(LOG_WARN, addrport, &address, &port)<0) {
  895. log_warn(LD_CONFIG, "Error parsing transport address '%s'", addrport);
  896. goto err;
  897. }
  898. if (!port) {
  899. log_warn(LD_CONFIG,
  900. "Transport address '%s' has no port.", addrport);
  901. goto err;
  902. }
  903. if (tor_addr_parse(&tor_addr, address) < 0) {
  904. log_warn(LD_CONFIG, "Error parsing transport address '%s'", address);
  905. goto err;
  906. }
  907. /** Check for options in the SMETHOD line. */
  908. if (is_smethod && smartlist_len(items) > min_args_count) {
  909. /* Seems like there are also some [options] in the SMETHOD line.
  910. Let's see if we can parse them. */
  911. char *options_string = smartlist_get(items, item_index);
  912. log_debug(LD_CONFIG, "Got options_string: %s", options_string);
  913. if (!strcmpstart(options_string, "ARGS:")) {
  914. args_string = options_string+strlen("ARGS:");
  915. log_debug(LD_CONFIG, "Got ARGS: %s", args_string);
  916. }
  917. }
  918. transport = transport_new(&tor_addr, port, transport_name,
  919. socks_ver, args_string);
  920. smartlist_add(mp->transports, transport);
  921. /** Logs info about line parsing success for client or server */
  922. if (is_smethod) {
  923. log_info(LD_CONFIG, "Server transport %s at %s:%d.",
  924. transport_name, address, (int)port);
  925. } else {
  926. log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
  927. "Attached to managed proxy.",
  928. transport_name, address, (int)port, socks_ver);
  929. }
  930. r=0;
  931. goto done;
  932. err:
  933. r = -1;
  934. done:
  935. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  936. smartlist_free(items);
  937. tor_free(address);
  938. return r;
  939. }
  940. /** Parses an SMETHOD <b>line</b> and if well-formed it registers the
  941. * new transport in <b>mp</b>. */
  942. STATIC int
  943. parse_smethod_line(const char *line, managed_proxy_t *mp)
  944. {
  945. /* Example of legit SMETHOD line:
  946. SMETHOD obfs2 0.0.0.0:25612 ARGS:secret=supersekrit,key=superkey */
  947. return parse_method_line_helper(line, mp, 1);
  948. }
  949. /** Parses a CMETHOD <b>line</b>, and if well-formed it registers
  950. * the new transport in <b>mp</b>. */
  951. STATIC int
  952. parse_cmethod_line(const char *line, managed_proxy_t *mp)
  953. {
  954. /* Example of legit CMETHOD line:
  955. CMETHOD obfs2 socks5 127.0.0.1:35713 */
  956. return parse_method_line_helper(line, mp, 0);
  957. }
  958. /** Parses an PROXY-ERROR <b>line</b> and warns the user accordingly. */
  959. STATIC void
  960. parse_proxy_error(const char *line)
  961. {
  962. /* (Length of the protocol string) plus (a space) and (the first char of
  963. the error message) */
  964. if (strlen(line) < (strlen(PROTO_PROXY_ERROR) + 2))
  965. log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
  966. "message.", PROTO_PROXY_ERROR);
  967. log_warn(LD_CONFIG, "Managed proxy failed to configure the "
  968. "pluggable transport's outgoing proxy. (%s)",
  969. line+strlen(PROTO_PROXY_ERROR)+1);
  970. }
  971. /** Parses a LOG <b>line</b> and emit log events accordingly. */
  972. STATIC void
  973. parse_log_line(const char *line, managed_proxy_t *mp)
  974. {
  975. tor_assert(line);
  976. tor_assert(mp);
  977. if (strlen(line) < (strlen(PROTO_LOG) + 1)) {
  978. log_warn(LD_PT, "Managed proxy sent us a %s line "
  979. "with missing argument.", PROTO_LOG);
  980. goto done;
  981. }
  982. const char *message = line + strlen(PROTO_LOG) + 1;
  983. log_info(LD_PT, "Managed proxy \"%s\" says: %s",
  984. mp->argv[0], message);
  985. /* Emit control port event. */
  986. control_event_pt_log(mp->argv[0], message);
  987. done:
  988. return;
  989. }
  990. /** Return a newly allocated string that tor should place in
  991. * TOR_PT_SERVER_TRANSPORT_OPTIONS while configuring the server
  992. * manged proxy in <b>mp</b>. Return NULL if no such options are found. */
  993. STATIC char *
  994. get_transport_options_for_server_proxy(const managed_proxy_t *mp)
  995. {
  996. char *options_string = NULL;
  997. smartlist_t *string_sl = smartlist_new();
  998. tor_assert(mp->is_server);
  999. /** Loop over the transports of the proxy. If we have options for
  1000. any of them, format them appropriately and place them in our
  1001. smartlist. Finally, join our smartlist to get the final
  1002. string. */
  1003. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, const char *, transport) {
  1004. smartlist_t *options_tmp_sl = NULL;
  1005. options_tmp_sl = get_options_for_server_transport(transport);
  1006. if (!options_tmp_sl)
  1007. continue;
  1008. /** Loop over the options of this transport, escape them, and
  1009. place them in the smartlist. */
  1010. SMARTLIST_FOREACH_BEGIN(options_tmp_sl, const char *, options) {
  1011. char *escaped_opts = tor_escape_str_for_pt_args(options, ":;\\");
  1012. smartlist_add_asprintf(string_sl, "%s:%s",
  1013. transport, escaped_opts);
  1014. tor_free(escaped_opts);
  1015. } SMARTLIST_FOREACH_END(options);
  1016. SMARTLIST_FOREACH(options_tmp_sl, char *, c, tor_free(c));
  1017. smartlist_free(options_tmp_sl);
  1018. } SMARTLIST_FOREACH_END(transport);
  1019. if (smartlist_len(string_sl)) {
  1020. options_string = smartlist_join_strings(string_sl, ";", 0, NULL);
  1021. }
  1022. SMARTLIST_FOREACH(string_sl, char *, t, tor_free(t));
  1023. smartlist_free(string_sl);
  1024. return options_string;
  1025. }
  1026. /** Return the string that tor should place in TOR_PT_SERVER_BINDADDR
  1027. * while configuring the server managed proxy in <b>mp</b>. The
  1028. * string is stored in the heap, and it's the responsibility of
  1029. * the caller to deallocate it after its use. */
  1030. static char *
  1031. get_bindaddr_for_server_proxy(const managed_proxy_t *mp)
  1032. {
  1033. char *bindaddr_result = NULL;
  1034. char *bindaddr_tmp = NULL;
  1035. smartlist_t *string_tmp = smartlist_new();
  1036. tor_assert(mp->is_server);
  1037. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
  1038. bindaddr_tmp = get_stored_bindaddr_for_server_transport(t);
  1039. smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);
  1040. tor_free(bindaddr_tmp);
  1041. } SMARTLIST_FOREACH_END(t);
  1042. bindaddr_result = smartlist_join_strings(string_tmp, ",", 0, NULL);
  1043. SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  1044. smartlist_free(string_tmp);
  1045. return bindaddr_result;
  1046. }
  1047. /** Return a newly allocated process_environment_t * for <b>mp</b>'s
  1048. * process. */
  1049. static smartlist_t *
  1050. create_managed_proxy_environment(const managed_proxy_t *mp)
  1051. {
  1052. const or_options_t *options = get_options();
  1053. /* Environment variables to be added to or set in mp's environment. */
  1054. smartlist_t *envs = smartlist_new();
  1055. /* XXXX The next time someone touches this code, shorten the name of
  1056. * set_environment_variable_in_smartlist, add a
  1057. * set_env_var_in_smartlist_asprintf function, and get rid of the
  1058. * silly extra envs smartlist. */
  1059. /* The final environment to be passed to mp. */
  1060. smartlist_t *merged_env_vars = get_current_process_environment_variables();
  1061. {
  1062. char *state_tmp = get_datadir_fname("pt_state/"); /* XXX temp */
  1063. smartlist_add_asprintf(envs, "TOR_PT_STATE_LOCATION=%s", state_tmp);
  1064. tor_free(state_tmp);
  1065. }
  1066. smartlist_add_strdup(envs, "TOR_PT_MANAGED_TRANSPORT_VER=1");
  1067. {
  1068. char *transports_to_launch =
  1069. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  1070. smartlist_add_asprintf(envs,
  1071. mp->is_server ?
  1072. "TOR_PT_SERVER_TRANSPORTS=%s" :
  1073. "TOR_PT_CLIENT_TRANSPORTS=%s",
  1074. transports_to_launch);
  1075. tor_free(transports_to_launch);
  1076. }
  1077. if (mp->is_server) {
  1078. {
  1079. char *orport_tmp =
  1080. get_first_listener_addrport_string(CONN_TYPE_OR_LISTENER);
  1081. if (orport_tmp) {
  1082. smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp);
  1083. tor_free(orport_tmp);
  1084. }
  1085. }
  1086. {
  1087. char *bindaddr_tmp = get_bindaddr_for_server_proxy(mp);
  1088. smartlist_add_asprintf(envs, "TOR_PT_SERVER_BINDADDR=%s", bindaddr_tmp);
  1089. tor_free(bindaddr_tmp);
  1090. }
  1091. {
  1092. char *server_transport_options =
  1093. get_transport_options_for_server_proxy(mp);
  1094. if (server_transport_options) {
  1095. smartlist_add_asprintf(envs, "TOR_PT_SERVER_TRANSPORT_OPTIONS=%s",
  1096. server_transport_options);
  1097. tor_free(server_transport_options);
  1098. }
  1099. }
  1100. /* XXXX Remove the '=' here once versions of obfsproxy which
  1101. * assert that this env var exists are sufficiently dead.
  1102. *
  1103. * (If we remove this line entirely, some joker will stick this
  1104. * variable in Tor's environment and crash PTs that try to parse
  1105. * it even when not run in server mode.) */
  1106. if (options->ExtORPort_lines) {
  1107. char *ext_or_addrport_tmp =
  1108. get_first_listener_addrport_string(CONN_TYPE_EXT_OR_LISTENER);
  1109. char *cookie_file_loc = get_ext_or_auth_cookie_file_name();
  1110. if (ext_or_addrport_tmp) {
  1111. smartlist_add_asprintf(envs, "TOR_PT_EXTENDED_SERVER_PORT=%s",
  1112. ext_or_addrport_tmp);
  1113. }
  1114. smartlist_add_asprintf(envs, "TOR_PT_AUTH_COOKIE_FILE=%s",
  1115. cookie_file_loc);
  1116. tor_free(ext_or_addrport_tmp);
  1117. tor_free(cookie_file_loc);
  1118. } else {
  1119. smartlist_add_asprintf(envs, "TOR_PT_EXTENDED_SERVER_PORT=");
  1120. }
  1121. /* All new versions of tor will keep stdin open, so PTs can use it
  1122. * as a reliable termination detection mechanism.
  1123. */
  1124. smartlist_add_asprintf(envs, "TOR_PT_EXIT_ON_STDIN_CLOSE=1");
  1125. } else {
  1126. /* If ClientTransportPlugin has a HTTPS/SOCKS proxy configured, set the
  1127. * TOR_PT_PROXY line.
  1128. */
  1129. if (mp->proxy_uri) {
  1130. smartlist_add_asprintf(envs, "TOR_PT_PROXY=%s", mp->proxy_uri);
  1131. }
  1132. }
  1133. SMARTLIST_FOREACH_BEGIN(envs, const char *, env_var) {
  1134. set_environment_variable_in_smartlist(merged_env_vars, env_var,
  1135. tor_free_, 1);
  1136. } SMARTLIST_FOREACH_END(env_var);
  1137. smartlist_free(envs);
  1138. return merged_env_vars;
  1139. }
  1140. /** Create and return a new managed proxy for <b>transport</b> using
  1141. * <b>proxy_argv</b>. Also, add it to the global managed proxy list. If
  1142. * <b>is_server</b> is true, it's a server managed proxy. Takes ownership of
  1143. * <b>proxy_argv</b>.
  1144. *
  1145. * Requires that proxy_argv have at least one element. */
  1146. STATIC managed_proxy_t *
  1147. managed_proxy_create(const smartlist_t *with_transport_list,
  1148. char **proxy_argv, int is_server)
  1149. {
  1150. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  1151. mp->conf_state = PT_PROTO_INFANT;
  1152. mp->is_server = is_server;
  1153. mp->argv = proxy_argv;
  1154. mp->transports = smartlist_new();
  1155. mp->proxy_uri = get_pt_proxy_uri();
  1156. mp->process = process_new(proxy_argv[0]);
  1157. mp->transports_to_launch = smartlist_new();
  1158. SMARTLIST_FOREACH(with_transport_list, const char *, transport,
  1159. add_transport_to_proxy(transport, mp));
  1160. /* register the managed proxy */
  1161. if (!managed_proxy_list)
  1162. managed_proxy_list = smartlist_new();
  1163. smartlist_add(managed_proxy_list, mp);
  1164. unconfigured_proxies_n++;
  1165. assert_unconfigured_count_ok();
  1166. return mp;
  1167. }
  1168. /** Register proxy with <b>proxy_argv</b>, supporting transports in
  1169. * <b>transport_list</b>, to the managed proxy subsystem.
  1170. * If <b>is_server</b> is true, then the proxy is a server proxy.
  1171. *
  1172. * Takes ownership of proxy_argv.
  1173. *
  1174. * Requires that proxy_argv be a NULL-terminated array of command-line
  1175. * elements, containing at least one element.
  1176. **/
  1177. MOCK_IMPL(void,
  1178. pt_kickstart_proxy, (const smartlist_t *with_transport_list,
  1179. char **proxy_argv, int is_server))
  1180. {
  1181. managed_proxy_t *mp=NULL;
  1182. transport_t *old_transport = NULL;
  1183. if (!proxy_argv || !proxy_argv[0]) {
  1184. return;
  1185. }
  1186. mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
  1187. if (!mp) { /* we haven't seen this proxy before */
  1188. managed_proxy_create(with_transport_list, proxy_argv, is_server);
  1189. } else { /* known proxy. add its transport to its transport list */
  1190. if (mp->was_around_before_config_read) {
  1191. /* If this managed proxy was around even before we read the
  1192. config this time, it means that it was already enabled before
  1193. and is not useless and should be kept. If it's marked for
  1194. removal, unmark it and make sure that we check whether it
  1195. needs to be restarted. */
  1196. if (mp->marked_for_removal) {
  1197. mp->marked_for_removal = 0;
  1198. check_if_restarts_needed = 1;
  1199. }
  1200. /* For each new transport, check if the managed proxy used to
  1201. support it before the SIGHUP. If that was the case, make sure
  1202. it doesn't get removed because we might reuse it. */
  1203. SMARTLIST_FOREACH_BEGIN(with_transport_list, const char *, transport) {
  1204. old_transport = transport_get_by_name(transport);
  1205. if (old_transport)
  1206. old_transport->marked_for_removal = 0;
  1207. } SMARTLIST_FOREACH_END(transport);
  1208. }
  1209. SMARTLIST_FOREACH(with_transport_list, const char *, transport,
  1210. add_transport_to_proxy(transport, mp));
  1211. free_execve_args(proxy_argv);
  1212. }
  1213. }
  1214. /** Frees the array of pointers in <b>arg</b> used as arguments to
  1215. execve(2). */
  1216. STATIC void
  1217. free_execve_args(char **arg)
  1218. {
  1219. char **tmp = arg;
  1220. while (*tmp) /* use the fact that the last element of the array is a
  1221. NULL pointer to know when to stop freeing */
  1222. tor_free_(*tmp++);
  1223. tor_free(arg);
  1224. }
  1225. /** Tor will read its config.
  1226. * Prepare the managed proxy list so that proxies not used in the new
  1227. * config will shutdown, and proxies that need to spawn different
  1228. * transports will do so. */
  1229. void
  1230. pt_prepare_proxy_list_for_config_read(void)
  1231. {
  1232. if (!managed_proxy_list)
  1233. return;
  1234. assert_unconfigured_count_ok();
  1235. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  1236. /* Destroy unconfigured proxies. */
  1237. if (mp->conf_state != PT_PROTO_COMPLETED) {
  1238. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  1239. managed_proxy_destroy(mp, 1);
  1240. unconfigured_proxies_n--;
  1241. continue;
  1242. }
  1243. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  1244. /* Mark all proxies for removal, and also note that they have been
  1245. here before the config read. */
  1246. mp->marked_for_removal = 1;
  1247. mp->was_around_before_config_read = 1;
  1248. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  1249. smartlist_clear(mp->transports_to_launch);
  1250. } SMARTLIST_FOREACH_END(mp);
  1251. assert_unconfigured_count_ok();
  1252. tor_assert(unconfigured_proxies_n == 0);
  1253. }
  1254. /** Return a smartlist containing the ports where our pluggable
  1255. * transports are listening. */
  1256. smartlist_t *
  1257. get_transport_proxy_ports(void)
  1258. {
  1259. smartlist_t *sl = NULL;
  1260. if (!managed_proxy_list)
  1261. return NULL;
  1262. /** XXX assume that external proxy ports have been forwarded
  1263. manually */
  1264. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, const managed_proxy_t *, mp) {
  1265. if (!mp->is_server || mp->conf_state != PT_PROTO_COMPLETED)
  1266. continue;
  1267. if (!sl) sl = smartlist_new();
  1268. tor_assert(mp->transports);
  1269. SMARTLIST_FOREACH(mp->transports, const transport_t *, t,
  1270. smartlist_add_asprintf(sl, "%u:%u", t->port, t->port));
  1271. } SMARTLIST_FOREACH_END(mp);
  1272. return sl;
  1273. }
  1274. /** Return the pluggable transport string that we should display in
  1275. * our extra-info descriptor. If we shouldn't display such a string,
  1276. * or we have nothing to display, return NULL. The string is
  1277. * allocated on the heap and it's the responsibility of the caller to
  1278. * free it. */
  1279. char *
  1280. pt_get_extra_info_descriptor_string(void)
  1281. {
  1282. char *the_string = NULL;
  1283. smartlist_t *string_chunks = NULL;
  1284. if (!managed_proxy_list)
  1285. return NULL;
  1286. string_chunks = smartlist_new();
  1287. /* For each managed proxy, add its transports to the chunks list. */
  1288. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, const managed_proxy_t *, mp) {
  1289. if ((!mp->is_server) || (mp->conf_state != PT_PROTO_COMPLETED))
  1290. continue;
  1291. tor_assert(mp->transports);
  1292. SMARTLIST_FOREACH_BEGIN(mp->transports, const transport_t *, t) {
  1293. char *transport_args = NULL;
  1294. /* If the transport proxy returned "0.0.0.0" as its address, and
  1295. * we know our external IP address, use it. Otherwise, use the
  1296. * returned address. */
  1297. const char *addrport = NULL;
  1298. uint32_t external_ip_address = 0;
  1299. if (tor_addr_is_null(&t->addr) &&
  1300. router_pick_published_address(get_options(),
  1301. &external_ip_address, 0) >= 0) {
  1302. tor_addr_t addr;
  1303. tor_addr_from_ipv4h(&addr, external_ip_address);
  1304. addrport = fmt_addrport(&addr, t->port);
  1305. } else {
  1306. addrport = fmt_addrport(&t->addr, t->port);
  1307. }
  1308. /* If this transport has any arguments with it, prepend a space
  1309. to them so that we can add them to the transport line. */
  1310. if (t->extra_info_args)
  1311. tor_asprintf(&transport_args, " %s", t->extra_info_args);
  1312. smartlist_add_asprintf(string_chunks,
  1313. "transport %s %s%s",
  1314. t->name, addrport,
  1315. transport_args ? transport_args : "");
  1316. tor_free(transport_args);
  1317. } SMARTLIST_FOREACH_END(t);
  1318. } SMARTLIST_FOREACH_END(mp);
  1319. if (smartlist_len(string_chunks) == 0) {
  1320. smartlist_free(string_chunks);
  1321. return NULL;
  1322. }
  1323. /* Join all the chunks into the final string. */
  1324. the_string = smartlist_join_strings(string_chunks, "\n", 1, NULL);
  1325. SMARTLIST_FOREACH(string_chunks, char *, s, tor_free(s));
  1326. smartlist_free(string_chunks);
  1327. return the_string;
  1328. }
  1329. /** Stringify the SOCKS arguments in <b>socks_args</b> according to
  1330. * 180_pluggable_transport.txt. The string is allocated on the heap
  1331. * and it's the responsibility of the caller to free it after use. */
  1332. char *
  1333. pt_stringify_socks_args(const smartlist_t *socks_args)
  1334. {
  1335. /* tmp place to store escaped socks arguments, so that we can
  1336. concatenate them up afterwards */
  1337. smartlist_t *sl_tmp = NULL;
  1338. char *escaped_string = NULL;
  1339. char *new_string = NULL;
  1340. tor_assert(socks_args);
  1341. tor_assert(smartlist_len(socks_args) > 0);
  1342. sl_tmp = smartlist_new();
  1343. SMARTLIST_FOREACH_BEGIN(socks_args, const char *, s) {
  1344. /* Escape ';' and '\'. */
  1345. escaped_string = tor_escape_str_for_pt_args(s, ";\\");
  1346. if (!escaped_string)
  1347. goto done;
  1348. smartlist_add(sl_tmp, escaped_string);
  1349. } SMARTLIST_FOREACH_END(s);
  1350. new_string = smartlist_join_strings(sl_tmp, ";", 0, NULL);
  1351. done:
  1352. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  1353. smartlist_free(sl_tmp);
  1354. return new_string;
  1355. }
  1356. /** Return a string of the SOCKS arguments that we should pass to the
  1357. * pluggable transports proxy in <b>addr</b>:<b>port</b> according to
  1358. * 180_pluggable_transport.txt. The string is allocated on the heap
  1359. * and it's the responsibility of the caller to free it after use. */
  1360. char *
  1361. pt_get_socks_args_for_proxy_addrport(const tor_addr_t *addr, uint16_t port)
  1362. {
  1363. const smartlist_t *socks_args = NULL;
  1364. socks_args = get_socks_args_by_bridge_addrport(addr, port);
  1365. if (!socks_args)
  1366. return NULL;
  1367. return pt_stringify_socks_args(socks_args);
  1368. }
  1369. /** The tor config was read.
  1370. * Destroy all managed proxies that were marked by a previous call to
  1371. * prepare_proxy_list_for_config_read() and are not used by the new
  1372. * config. */
  1373. void
  1374. sweep_proxy_list(void)
  1375. {
  1376. if (!managed_proxy_list)
  1377. return;
  1378. assert_unconfigured_count_ok();
  1379. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  1380. if (mp->marked_for_removal) {
  1381. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  1382. managed_proxy_destroy(mp, 1);
  1383. }
  1384. } SMARTLIST_FOREACH_END(mp);
  1385. assert_unconfigured_count_ok();
  1386. }
  1387. /** Release all storage held by the pluggable transports subsystem. */
  1388. void
  1389. pt_free_all(void)
  1390. {
  1391. if (transport_list) {
  1392. clear_transport_list();
  1393. smartlist_free(transport_list);
  1394. transport_list = NULL;
  1395. }
  1396. if (managed_proxy_list) {
  1397. /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
  1398. transports and it's the duty of the circuitbuild.c subsystem to
  1399. free them. Otherwise, it hasn't registered its transports yet
  1400. and we should free them here. */
  1401. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
  1402. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  1403. managed_proxy_destroy(mp, 1);
  1404. });
  1405. smartlist_free(managed_proxy_list);
  1406. managed_proxy_list=NULL;
  1407. }
  1408. }
  1409. /** Return a newly allocated string equal to <b>string</b>, except that every
  1410. * character in <b>chars_to_escape</b> is preceded by a backslash. */
  1411. char *
  1412. tor_escape_str_for_pt_args(const char *string, const char *chars_to_escape)
  1413. {
  1414. char *new_string = NULL;
  1415. char *new_cp = NULL;
  1416. size_t length, new_length;
  1417. tor_assert(string);
  1418. length = strlen(string);
  1419. if (!length) /* If we were given the empty string, return the same. */
  1420. return tor_strdup("");
  1421. /* (new_length > SIZE_MAX) => ((length * 2) + 1 > SIZE_MAX) =>
  1422. (length*2 > SIZE_MAX - 1) => (length > (SIZE_MAX - 1)/2) */
  1423. if (length > (SIZE_MAX - 1)/2) /* check for overflow */
  1424. return NULL;
  1425. /* this should be enough even if all characters must be escaped */
  1426. new_length = (length * 2) + 1;
  1427. new_string = new_cp = tor_malloc(new_length);
  1428. while (*string) {
  1429. if (strchr(chars_to_escape, *string))
  1430. *new_cp++ = '\\';
  1431. *new_cp++ = *string++;
  1432. }
  1433. *new_cp = '\0'; /* NUL-terminate the new string */
  1434. return new_string;
  1435. }
  1436. /** Callback function that is called when our PT process have data on its
  1437. * stdout. Our process can be found in <b>process</b>, the data can be found in
  1438. * <b>line</b> and the length of our line is given in <b>size</b>. */
  1439. STATIC void
  1440. managed_proxy_stdout_callback(process_t *process,
  1441. const char *line,
  1442. size_t size)
  1443. {
  1444. tor_assert(process);
  1445. tor_assert(line);
  1446. (void)size;
  1447. managed_proxy_t *mp = process_get_data(process);
  1448. handle_proxy_line(line, mp);
  1449. if (proxy_configuration_finished(mp)) {
  1450. handle_finished_proxy(mp);
  1451. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  1452. }
  1453. }
  1454. /** Callback function that is called when our PT process have data on its
  1455. * stderr. Our process can be found in <b>process</b>, the data can be found in
  1456. * <b>line</b> and the length of our line is given in <b>size</b>. */
  1457. STATIC void
  1458. managed_proxy_stderr_callback(process_t *process,
  1459. const char *line,
  1460. size_t size)
  1461. {
  1462. tor_assert(process);
  1463. tor_assert(line);
  1464. (void)size;
  1465. managed_proxy_t *mp = process_get_data(process);
  1466. log_warn(LD_PT, "Managed proxy at '%s' reported: %s", mp->argv[0], line);
  1467. }
  1468. /** Callback function that is called when our PT process terminates. The
  1469. * process exit code can be found in <b>exit_code</b> and our process can be
  1470. * found in <b>process</b>. Returns true iff we want the process subsystem to
  1471. * free our process_t handle for us. */
  1472. STATIC bool
  1473. managed_proxy_exit_callback(process_t *process, process_exit_code_t exit_code)
  1474. {
  1475. tor_assert(process);
  1476. log_warn(LD_PT,
  1477. "Pluggable Transport process terminated with status code %" PRIu64,
  1478. exit_code);
  1479. /* We detach ourself from the MP (if we are attached) and free ourself. */
  1480. managed_proxy_t *mp = process_get_data(process);
  1481. /* If we are still attached to the process, it is probably because our PT
  1482. * process crashed before we got to call process_set_data(p, NULL); */
  1483. if (BUG(mp != NULL)) {
  1484. /* FIXME(ahf): Our process stopped without us having told it to stop
  1485. * (crashed). Should we restart it here? */
  1486. mp->process = NULL;
  1487. process_set_data(process, NULL);
  1488. }
  1489. return true;
  1490. }