transports.c 50 KB

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