transports.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /* Copyright (c) 2011-2016, 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
  462. retval = tor_spawn_background(mp->argv[0],
  463. (const char **)mp->argv,
  464. env,
  465. &mp->process_handle);
  466. #endif
  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. futher. */
  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. if (!transport)
  936. goto err;
  937. smartlist_add(mp->transports, transport);
  938. /* For now, notify the user so that they know where the server
  939. transport is listening. */
  940. log_info(LD_CONFIG, "Server transport %s at %s:%d.",
  941. method_name, address, (int)port);
  942. r=0;
  943. goto done;
  944. err:
  945. r = -1;
  946. done:
  947. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  948. smartlist_free(items);
  949. tor_free(address);
  950. return r;
  951. }
  952. /** Parses a CMETHOD <b>line</b>, and if well-formed it registers
  953. * the new transport in <b>mp</b>. */
  954. STATIC int
  955. parse_cmethod_line(const char *line, managed_proxy_t *mp)
  956. {
  957. int r;
  958. smartlist_t *items = NULL;
  959. char *method_name=NULL;
  960. char *socks_ver_str=NULL;
  961. int socks_ver=PROXY_NONE;
  962. char *addrport=NULL;
  963. tor_addr_t tor_addr;
  964. char *address=NULL;
  965. uint16_t port = 0;
  966. transport_t *transport=NULL;
  967. items = smartlist_new();
  968. smartlist_split_string(items, line, NULL,
  969. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  970. if (smartlist_len(items) < 4) {
  971. log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
  972. "with too few arguments.");
  973. goto err;
  974. }
  975. tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
  976. method_name = smartlist_get(items,1);
  977. if (!string_is_C_identifier(method_name)) {
  978. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  979. method_name);
  980. goto err;
  981. }
  982. socks_ver_str = smartlist_get(items,2);
  983. if (!strcmp(socks_ver_str,"socks4")) {
  984. socks_ver = PROXY_SOCKS4;
  985. } else if (!strcmp(socks_ver_str,"socks5")) {
  986. socks_ver = PROXY_SOCKS5;
  987. } else {
  988. log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
  989. "we don't recognize. (%s)", socks_ver_str);
  990. goto err;
  991. }
  992. addrport = smartlist_get(items, 3);
  993. if (tor_addr_port_split(LOG_WARN, addrport, &address, &port)<0) {
  994. log_warn(LD_CONFIG, "Error parsing transport "
  995. "address '%s'", addrport);
  996. goto err;
  997. }
  998. if (!port) {
  999. log_warn(LD_CONFIG,
  1000. "Transport address '%s' has no port.", addrport);
  1001. goto err;
  1002. }
  1003. if (tor_addr_parse(&tor_addr, address) < 0) {
  1004. log_warn(LD_CONFIG, "Error parsing transport address '%s'", address);
  1005. goto err;
  1006. }
  1007. transport = transport_new(&tor_addr, port, method_name, socks_ver, NULL);
  1008. if (!transport)
  1009. goto err;
  1010. smartlist_add(mp->transports, transport);
  1011. log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
  1012. "Attached to managed proxy.",
  1013. method_name, address, (int)port, socks_ver);
  1014. r=0;
  1015. goto done;
  1016. err:
  1017. r = -1;
  1018. done:
  1019. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  1020. smartlist_free(items);
  1021. tor_free(address);
  1022. return r;
  1023. }
  1024. /** Parses an PROXY-ERROR <b>line</b> and warns the user accordingly. */
  1025. STATIC void
  1026. parse_proxy_error(const char *line)
  1027. {
  1028. /* (Length of the protocol string) plus (a space) and (the first char of
  1029. the error message) */
  1030. if (strlen(line) < (strlen(PROTO_PROXY_ERROR) + 2))
  1031. log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
  1032. "message.", PROTO_PROXY_ERROR);
  1033. log_warn(LD_CONFIG, "Managed proxy failed to configure the "
  1034. "pluggable transport's outgoing proxy. (%s)",
  1035. line+strlen(PROTO_PROXY_ERROR)+1);
  1036. }
  1037. /** Return a newly allocated string that tor should place in
  1038. * TOR_PT_SERVER_TRANSPORT_OPTIONS while configuring the server
  1039. * manged proxy in <b>mp</b>. Return NULL if no such options are found. */
  1040. STATIC char *
  1041. get_transport_options_for_server_proxy(const managed_proxy_t *mp)
  1042. {
  1043. char *options_string = NULL;
  1044. smartlist_t *string_sl = smartlist_new();
  1045. tor_assert(mp->is_server);
  1046. /** Loop over the transports of the proxy. If we have options for
  1047. any of them, format them appropriately and place them in our
  1048. smartlist. Finally, join our smartlist to get the final
  1049. string. */
  1050. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, const char *, transport) {
  1051. smartlist_t *options_tmp_sl = NULL;
  1052. options_tmp_sl = get_options_for_server_transport(transport);
  1053. if (!options_tmp_sl)
  1054. continue;
  1055. /** Loop over the options of this transport, escape them, and
  1056. place them in the smartlist. */
  1057. SMARTLIST_FOREACH_BEGIN(options_tmp_sl, const char *, options) {
  1058. char *escaped_opts = tor_escape_str_for_pt_args(options, ":;\\");
  1059. smartlist_add_asprintf(string_sl, "%s:%s",
  1060. transport, escaped_opts);
  1061. tor_free(escaped_opts);
  1062. } SMARTLIST_FOREACH_END(options);
  1063. SMARTLIST_FOREACH(options_tmp_sl, char *, c, tor_free(c));
  1064. smartlist_free(options_tmp_sl);
  1065. } SMARTLIST_FOREACH_END(transport);
  1066. if (smartlist_len(string_sl)) {
  1067. options_string = smartlist_join_strings(string_sl, ";", 0, NULL);
  1068. }
  1069. SMARTLIST_FOREACH(string_sl, char *, t, tor_free(t));
  1070. smartlist_free(string_sl);
  1071. return options_string;
  1072. }
  1073. /** Return the string that tor should place in TOR_PT_SERVER_BINDADDR
  1074. * while configuring the server managed proxy in <b>mp</b>. The
  1075. * string is stored in the heap, and it's the responsibility of
  1076. * the caller to deallocate it after its use. */
  1077. static char *
  1078. get_bindaddr_for_server_proxy(const managed_proxy_t *mp)
  1079. {
  1080. char *bindaddr_result = NULL;
  1081. char *bindaddr_tmp = NULL;
  1082. smartlist_t *string_tmp = smartlist_new();
  1083. tor_assert(mp->is_server);
  1084. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
  1085. bindaddr_tmp = get_stored_bindaddr_for_server_transport(t);
  1086. smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);
  1087. tor_free(bindaddr_tmp);
  1088. } SMARTLIST_FOREACH_END(t);
  1089. bindaddr_result = smartlist_join_strings(string_tmp, ",", 0, NULL);
  1090. SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  1091. smartlist_free(string_tmp);
  1092. return bindaddr_result;
  1093. }
  1094. /** Return a newly allocated process_environment_t * for <b>mp</b>'s
  1095. * process. */
  1096. static process_environment_t *
  1097. create_managed_proxy_environment(const managed_proxy_t *mp)
  1098. {
  1099. const or_options_t *options = get_options();
  1100. /* Environment variables to be added to or set in mp's environment. */
  1101. smartlist_t *envs = smartlist_new();
  1102. /* XXXX The next time someone touches this code, shorten the name of
  1103. * set_environment_variable_in_smartlist, add a
  1104. * set_env_var_in_smartlist_asprintf function, and get rid of the
  1105. * silly extra envs smartlist. */
  1106. /* The final environment to be passed to mp. */
  1107. smartlist_t *merged_env_vars = get_current_process_environment_variables();
  1108. process_environment_t *env;
  1109. {
  1110. char *state_tmp = get_datadir_fname("pt_state/"); /* XXX temp */
  1111. smartlist_add_asprintf(envs, "TOR_PT_STATE_LOCATION=%s", state_tmp);
  1112. tor_free(state_tmp);
  1113. }
  1114. smartlist_add_strdup(envs, "TOR_PT_MANAGED_TRANSPORT_VER=1");
  1115. {
  1116. char *transports_to_launch =
  1117. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  1118. smartlist_add_asprintf(envs,
  1119. mp->is_server ?
  1120. "TOR_PT_SERVER_TRANSPORTS=%s" :
  1121. "TOR_PT_CLIENT_TRANSPORTS=%s",
  1122. transports_to_launch);
  1123. tor_free(transports_to_launch);
  1124. }
  1125. if (mp->is_server) {
  1126. {
  1127. char *orport_tmp =
  1128. get_first_listener_addrport_string(CONN_TYPE_OR_LISTENER);
  1129. if (orport_tmp) {
  1130. smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp);
  1131. tor_free(orport_tmp);
  1132. }
  1133. }
  1134. {
  1135. char *bindaddr_tmp = get_bindaddr_for_server_proxy(mp);
  1136. smartlist_add_asprintf(envs, "TOR_PT_SERVER_BINDADDR=%s", bindaddr_tmp);
  1137. tor_free(bindaddr_tmp);
  1138. }
  1139. {
  1140. char *server_transport_options =
  1141. get_transport_options_for_server_proxy(mp);
  1142. if (server_transport_options) {
  1143. smartlist_add_asprintf(envs, "TOR_PT_SERVER_TRANSPORT_OPTIONS=%s",
  1144. server_transport_options);
  1145. tor_free(server_transport_options);
  1146. }
  1147. }
  1148. /* XXXX Remove the '=' here once versions of obfsproxy which
  1149. * assert that this env var exists are sufficiently dead.
  1150. *
  1151. * (If we remove this line entirely, some joker will stick this
  1152. * variable in Tor's environment and crash PTs that try to parse
  1153. * it even when not run in server mode.) */
  1154. if (options->ExtORPort_lines) {
  1155. char *ext_or_addrport_tmp =
  1156. get_first_listener_addrport_string(CONN_TYPE_EXT_OR_LISTENER);
  1157. char *cookie_file_loc = get_ext_or_auth_cookie_file_name();
  1158. if (ext_or_addrport_tmp) {
  1159. smartlist_add_asprintf(envs, "TOR_PT_EXTENDED_SERVER_PORT=%s",
  1160. ext_or_addrport_tmp);
  1161. }
  1162. smartlist_add_asprintf(envs, "TOR_PT_AUTH_COOKIE_FILE=%s",
  1163. cookie_file_loc);
  1164. tor_free(ext_or_addrport_tmp);
  1165. tor_free(cookie_file_loc);
  1166. } else {
  1167. smartlist_add_asprintf(envs, "TOR_PT_EXTENDED_SERVER_PORT=");
  1168. }
  1169. /* All new versions of tor will keep stdin open, so PTs can use it
  1170. * as a reliable termination detection mechanism.
  1171. */
  1172. smartlist_add_asprintf(envs, "TOR_PT_EXIT_ON_STDIN_CLOSE=1");
  1173. } else {
  1174. /* If ClientTransportPlugin has a HTTPS/SOCKS proxy configured, set the
  1175. * TOR_PT_PROXY line.
  1176. */
  1177. if (mp->proxy_uri) {
  1178. smartlist_add_asprintf(envs, "TOR_PT_PROXY=%s", mp->proxy_uri);
  1179. }
  1180. }
  1181. SMARTLIST_FOREACH_BEGIN(envs, const char *, env_var) {
  1182. set_environment_variable_in_smartlist(merged_env_vars, env_var,
  1183. tor_free_, 1);
  1184. } SMARTLIST_FOREACH_END(env_var);
  1185. env = process_environment_make(merged_env_vars);
  1186. smartlist_free(envs);
  1187. SMARTLIST_FOREACH(merged_env_vars, void *, x, tor_free(x));
  1188. smartlist_free(merged_env_vars);
  1189. return env;
  1190. }
  1191. /** Create and return a new managed proxy for <b>transport</b> using
  1192. * <b>proxy_argv</b>. Also, add it to the global managed proxy list. If
  1193. * <b>is_server</b> is true, it's a server managed proxy. Takes ownership of
  1194. * <b>proxy_argv</b>.
  1195. *
  1196. * Requires that proxy_argv have at least one element. */
  1197. STATIC managed_proxy_t *
  1198. managed_proxy_create(const smartlist_t *with_transport_list,
  1199. char **proxy_argv, int is_server)
  1200. {
  1201. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  1202. mp->conf_state = PT_PROTO_INFANT;
  1203. mp->is_server = is_server;
  1204. mp->argv = proxy_argv;
  1205. mp->transports = smartlist_new();
  1206. mp->proxy_uri = get_pt_proxy_uri();
  1207. mp->transports_to_launch = smartlist_new();
  1208. SMARTLIST_FOREACH(with_transport_list, const char *, transport,
  1209. add_transport_to_proxy(transport, mp));
  1210. /* register the managed proxy */
  1211. if (!managed_proxy_list)
  1212. managed_proxy_list = smartlist_new();
  1213. smartlist_add(managed_proxy_list, mp);
  1214. unconfigured_proxies_n++;
  1215. assert_unconfigured_count_ok();
  1216. return mp;
  1217. }
  1218. /** Register proxy with <b>proxy_argv</b>, supporting transports in
  1219. * <b>transport_list</b>, to the managed proxy subsystem.
  1220. * If <b>is_server</b> is true, then the proxy is a server proxy.
  1221. *
  1222. * Takes ownership of proxy_argv.
  1223. *
  1224. * Requires that proxy_argv be a NULL-terminated array of command-line
  1225. * elements, containing at least one element.
  1226. **/
  1227. MOCK_IMPL(void,
  1228. pt_kickstart_proxy, (const smartlist_t *with_transport_list,
  1229. char **proxy_argv, int is_server))
  1230. {
  1231. managed_proxy_t *mp=NULL;
  1232. transport_t *old_transport = NULL;
  1233. if (!proxy_argv || !proxy_argv[0]) {
  1234. return;
  1235. }
  1236. mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
  1237. if (!mp) { /* we haven't seen this proxy before */
  1238. managed_proxy_create(with_transport_list, proxy_argv, is_server);
  1239. } else { /* known proxy. add its transport to its transport list */
  1240. if (mp->was_around_before_config_read) {
  1241. /* If this managed proxy was around even before we read the
  1242. config this time, it means that it was already enabled before
  1243. and is not useless and should be kept. If it's marked for
  1244. removal, unmark it and make sure that we check whether it
  1245. needs to be restarted. */
  1246. if (mp->marked_for_removal) {
  1247. mp->marked_for_removal = 0;
  1248. check_if_restarts_needed = 1;
  1249. }
  1250. /* For each new transport, check if the managed proxy used to
  1251. support it before the SIGHUP. If that was the case, make sure
  1252. it doesn't get removed because we might reuse it. */
  1253. SMARTLIST_FOREACH_BEGIN(with_transport_list, const char *, transport) {
  1254. old_transport = transport_get_by_name(transport);
  1255. if (old_transport)
  1256. old_transport->marked_for_removal = 0;
  1257. } SMARTLIST_FOREACH_END(transport);
  1258. }
  1259. SMARTLIST_FOREACH(with_transport_list, const char *, transport,
  1260. add_transport_to_proxy(transport, mp));
  1261. free_execve_args(proxy_argv);
  1262. }
  1263. }
  1264. /** Frees the array of pointers in <b>arg</b> used as arguments to
  1265. execve(2). */
  1266. STATIC void
  1267. free_execve_args(char **arg)
  1268. {
  1269. char **tmp = arg;
  1270. while (*tmp) /* use the fact that the last element of the array is a
  1271. NULL pointer to know when to stop freeing */
  1272. tor_free_(*tmp++);
  1273. tor_free(arg);
  1274. }
  1275. /** Tor will read its config.
  1276. * Prepare the managed proxy list so that proxies not used in the new
  1277. * config will shutdown, and proxies that need to spawn different
  1278. * transports will do so. */
  1279. void
  1280. pt_prepare_proxy_list_for_config_read(void)
  1281. {
  1282. if (!managed_proxy_list)
  1283. return;
  1284. assert_unconfigured_count_ok();
  1285. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  1286. /* Destroy unconfigured proxies. */
  1287. if (mp->conf_state != PT_PROTO_COMPLETED) {
  1288. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  1289. managed_proxy_destroy(mp, 1);
  1290. unconfigured_proxies_n--;
  1291. continue;
  1292. }
  1293. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  1294. /* Mark all proxies for removal, and also note that they have been
  1295. here before the config read. */
  1296. mp->marked_for_removal = 1;
  1297. mp->was_around_before_config_read = 1;
  1298. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  1299. smartlist_clear(mp->transports_to_launch);
  1300. } SMARTLIST_FOREACH_END(mp);
  1301. assert_unconfigured_count_ok();
  1302. tor_assert(unconfigured_proxies_n == 0);
  1303. }
  1304. /** Return a smartlist containing the ports where our pluggable
  1305. * transports are listening. */
  1306. smartlist_t *
  1307. get_transport_proxy_ports(void)
  1308. {
  1309. smartlist_t *sl = NULL;
  1310. if (!managed_proxy_list)
  1311. return NULL;
  1312. /** XXX assume that external proxy ports have been forwarded
  1313. manually */
  1314. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, const managed_proxy_t *, mp) {
  1315. if (!mp->is_server || mp->conf_state != PT_PROTO_COMPLETED)
  1316. continue;
  1317. if (!sl) sl = smartlist_new();
  1318. tor_assert(mp->transports);
  1319. SMARTLIST_FOREACH(mp->transports, const transport_t *, t,
  1320. smartlist_add_asprintf(sl, "%u:%u", t->port, t->port));
  1321. } SMARTLIST_FOREACH_END(mp);
  1322. return sl;
  1323. }
  1324. /** Return the pluggable transport string that we should display in
  1325. * our extra-info descriptor. If we shouldn't display such a string,
  1326. * or we have nothing to display, return NULL. The string is
  1327. * allocated on the heap and it's the responsibility of the caller to
  1328. * free it. */
  1329. char *
  1330. pt_get_extra_info_descriptor_string(void)
  1331. {
  1332. char *the_string = NULL;
  1333. smartlist_t *string_chunks = NULL;
  1334. if (!managed_proxy_list)
  1335. return NULL;
  1336. string_chunks = smartlist_new();
  1337. /* For each managed proxy, add its transports to the chunks list. */
  1338. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, const managed_proxy_t *, mp) {
  1339. if ((!mp->is_server) || (mp->conf_state != PT_PROTO_COMPLETED))
  1340. continue;
  1341. tor_assert(mp->transports);
  1342. SMARTLIST_FOREACH_BEGIN(mp->transports, const transport_t *, t) {
  1343. char *transport_args = NULL;
  1344. /* If the transport proxy returned "0.0.0.0" as its address, and
  1345. * we know our external IP address, use it. Otherwise, use the
  1346. * returned address. */
  1347. const char *addrport = NULL;
  1348. uint32_t external_ip_address = 0;
  1349. if (tor_addr_is_null(&t->addr) &&
  1350. router_pick_published_address(get_options(),
  1351. &external_ip_address, 0) >= 0) {
  1352. tor_addr_t addr;
  1353. tor_addr_from_ipv4h(&addr, external_ip_address);
  1354. addrport = fmt_addrport(&addr, t->port);
  1355. } else {
  1356. addrport = fmt_addrport(&t->addr, t->port);
  1357. }
  1358. /* If this transport has any arguments with it, prepend a space
  1359. to them so that we can add them to the transport line. */
  1360. if (t->extra_info_args)
  1361. tor_asprintf(&transport_args, " %s", t->extra_info_args);
  1362. smartlist_add_asprintf(string_chunks,
  1363. "transport %s %s%s",
  1364. t->name, addrport,
  1365. transport_args ? transport_args : "");
  1366. tor_free(transport_args);
  1367. } SMARTLIST_FOREACH_END(t);
  1368. } SMARTLIST_FOREACH_END(mp);
  1369. if (smartlist_len(string_chunks) == 0) {
  1370. smartlist_free(string_chunks);
  1371. return NULL;
  1372. }
  1373. /* Join all the chunks into the final string. */
  1374. the_string = smartlist_join_strings(string_chunks, "\n", 1, NULL);
  1375. SMARTLIST_FOREACH(string_chunks, char *, s, tor_free(s));
  1376. smartlist_free(string_chunks);
  1377. return the_string;
  1378. }
  1379. /** Stringify the SOCKS arguments in <b>socks_args</b> according to
  1380. * 180_pluggable_transport.txt. The string is allocated on the heap
  1381. * and it's the responsibility of the caller to free it after use. */
  1382. char *
  1383. pt_stringify_socks_args(const smartlist_t *socks_args)
  1384. {
  1385. /* tmp place to store escaped socks arguments, so that we can
  1386. concatenate them up afterwards */
  1387. smartlist_t *sl_tmp = NULL;
  1388. char *escaped_string = NULL;
  1389. char *new_string = NULL;
  1390. tor_assert(socks_args);
  1391. tor_assert(smartlist_len(socks_args) > 0);
  1392. sl_tmp = smartlist_new();
  1393. SMARTLIST_FOREACH_BEGIN(socks_args, const char *, s) {
  1394. /* Escape ';' and '\'. */
  1395. escaped_string = tor_escape_str_for_pt_args(s, ";\\");
  1396. if (!escaped_string)
  1397. goto done;
  1398. smartlist_add(sl_tmp, escaped_string);
  1399. } SMARTLIST_FOREACH_END(s);
  1400. new_string = smartlist_join_strings(sl_tmp, ";", 0, NULL);
  1401. done:
  1402. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  1403. smartlist_free(sl_tmp);
  1404. return new_string;
  1405. }
  1406. /** Return a string of the SOCKS arguments that we should pass to the
  1407. * pluggable transports proxy in <b>addr</b>:<b>port</b> according to
  1408. * 180_pluggable_transport.txt. The string is allocated on the heap
  1409. * and it's the responsibility of the caller to free it after use. */
  1410. char *
  1411. pt_get_socks_args_for_proxy_addrport(const tor_addr_t *addr, uint16_t port)
  1412. {
  1413. const smartlist_t *socks_args = NULL;
  1414. socks_args = get_socks_args_by_bridge_addrport(addr, port);
  1415. if (!socks_args)
  1416. return NULL;
  1417. return pt_stringify_socks_args(socks_args);
  1418. }
  1419. /** The tor config was read.
  1420. * Destroy all managed proxies that were marked by a previous call to
  1421. * prepare_proxy_list_for_config_read() and are not used by the new
  1422. * config. */
  1423. void
  1424. sweep_proxy_list(void)
  1425. {
  1426. if (!managed_proxy_list)
  1427. return;
  1428. assert_unconfigured_count_ok();
  1429. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  1430. if (mp->marked_for_removal) {
  1431. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  1432. managed_proxy_destroy(mp, 1);
  1433. }
  1434. } SMARTLIST_FOREACH_END(mp);
  1435. assert_unconfigured_count_ok();
  1436. }
  1437. /** Release all storage held by the pluggable transports subsystem. */
  1438. void
  1439. pt_free_all(void)
  1440. {
  1441. if (transport_list) {
  1442. clear_transport_list();
  1443. smartlist_free(transport_list);
  1444. transport_list = NULL;
  1445. }
  1446. if (managed_proxy_list) {
  1447. /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
  1448. transports and it's the duty of the circuitbuild.c subsystem to
  1449. free them. Otherwise, it hasn't registered its transports yet
  1450. and we should free them here. */
  1451. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp, {
  1452. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  1453. managed_proxy_destroy(mp, 1);
  1454. });
  1455. smartlist_free(managed_proxy_list);
  1456. managed_proxy_list=NULL;
  1457. }
  1458. }