transports.c 51 KB

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