transports.c 51 KB

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