transports.c 55 KB

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