transports.c 54 KB

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