2011-07-13 19:00:28 +02:00
|
|
|
/* Copyright (c) 2011, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
2011-07-18 02:33:31 +02:00
|
|
|
* \file transports.c
|
2011-07-13 19:00:28 +02:00
|
|
|
* \brief Pluggable Transports related code.
|
2011-12-16 11:01:56 +01:00
|
|
|
*
|
|
|
|
* \details
|
|
|
|
* Each managed proxy is represented by a <b>managed_proxy_t</b>.
|
|
|
|
* Each managed proxy can support multiple transports.
|
|
|
|
* Each managed proxy gets configured through a multistep process.
|
|
|
|
*
|
|
|
|
* ::managed_proxy_list contains all the managed proxies this tor
|
|
|
|
* instance is supporting.
|
|
|
|
* In the ::managed_proxy_list there are ::unconfigured_proxies_n
|
|
|
|
* managed proxies that are still unconfigured.
|
|
|
|
*
|
|
|
|
* In every run_scheduled_event() tick, we attempt to launch and then
|
|
|
|
* configure the unconfiged managed proxies, using the configuration
|
|
|
|
* protocol defined in the 180_pluggable_transport.txt proposal. A
|
|
|
|
* managed proxy might need several ticks to get fully configured.
|
|
|
|
*
|
|
|
|
* When a managed proxy is fully configured, we register all its
|
|
|
|
* transports to the circuitbuild.c subsystem. At that point the
|
|
|
|
* transports are owned by the circuitbuild.c subsystem.
|
|
|
|
*
|
|
|
|
* When a managed proxy fails to follow the 180 configuration
|
|
|
|
* protocol, it gets marked as broken and gets destroyed.
|
|
|
|
*
|
|
|
|
* <b>In a little more detail:</b>
|
|
|
|
*
|
|
|
|
* While we are serially parsing torrc, we store all the transports
|
|
|
|
* that a proxy should spawn in its <em>transports_to_launch</em>
|
|
|
|
* element.
|
|
|
|
*
|
|
|
|
* When we finish reading the torrc, we spawn the managed proxy and
|
|
|
|
* expect {S,C}METHOD lines from its output. We add transports
|
|
|
|
* described by METHOD lines to its <em>transports</em> element, as
|
|
|
|
* transport_t structs.
|
|
|
|
*
|
|
|
|
* When the managed proxy stops spitting METHOD lines (signified by a
|
|
|
|
* '{S,C}METHODS DONE' message) we register all the transports
|
|
|
|
* collected to the circuitbuild.c subsystem. At this point, the
|
|
|
|
* pointers to transport_t can be transformed into dangling pointers
|
|
|
|
* at any point by the circuitbuild.c subsystem, and so we replace all
|
|
|
|
* transport_t pointers with strings describing the transport names.
|
|
|
|
* We can still go from a transport name to a transport_t using the
|
|
|
|
* fact that each transport name uniquely identifies a transport_t.
|
|
|
|
*
|
|
|
|
* <b>In even more detail, this is what happens when a SIGHUP
|
|
|
|
* occurs:</b>
|
|
|
|
*
|
|
|
|
* We immediately destroy all unconfigured proxies (We shouldn't have
|
|
|
|
* unconfigured proxies in the first place, except when SIGHUP rings
|
|
|
|
* immediately after tor is launched.).
|
|
|
|
*
|
|
|
|
* We mark all managed proxies and transports to signify that they
|
|
|
|
* must be removed if they don't contribute by the new torrc
|
|
|
|
* (we mark using the <b>marked_for_removal</b> element).
|
|
|
|
* We also mark all managed proxies to signify that they might need to
|
|
|
|
* be restarted so that they end up supporting all the transports the
|
|
|
|
* new torrc wants them to support (using the <b>got_hup</b> element).
|
|
|
|
* We also clear their <b>transports_to_launch</b> list so that we can
|
|
|
|
* put there the transports we need to launch according to the new
|
|
|
|
* torrc.
|
|
|
|
*
|
|
|
|
* We then start parsing torrc again.
|
|
|
|
*
|
|
|
|
* Everytime we encounter a transport line using a known pre-SIGHUP
|
|
|
|
* managed proxy, we cleanse that proxy from the removal mark.
|
|
|
|
* We also mark it as unconfigured so that on the next scheduled
|
|
|
|
* events tick, we investigate whether we need to restart the proxy
|
|
|
|
* so that it also spawns the new transports.
|
|
|
|
* If the post-SIGHUP <b>transports_to_launch</b> list is identical to
|
|
|
|
* the pre-SIGHUP one, it means that no changes were introduced to
|
|
|
|
* this proxy during the SIGHUP and no restart has to take place.
|
|
|
|
*
|
|
|
|
* During the post-SIGHUP torrc parsing, we unmark all transports
|
|
|
|
* spawned by managed proxies that we find in our torrc.
|
|
|
|
* We do that so that if we don't need to restart a managed proxy, we
|
|
|
|
* can continue using its old transports normally.
|
|
|
|
* If we end up restarting the proxy, we destroy and unregister all
|
|
|
|
* old transports from the circuitbuild.c subsystem.
|
2011-07-13 19:00:28 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#define PT_PRIVATE
|
|
|
|
#include "or.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "circuitbuild.h"
|
2011-07-18 02:33:31 +02:00
|
|
|
#include "transports.h"
|
2011-08-15 17:26:03 +02:00
|
|
|
#include "util.h"
|
2011-11-08 22:10:38 +01:00
|
|
|
#include "router.h"
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2011-10-24 16:01:24 +02:00
|
|
|
static void set_managed_proxy_environment(LPVOID *envp,
|
2011-09-11 23:34:36 +02:00
|
|
|
const managed_proxy_t *mp);
|
2011-10-17 22:46:44 +02:00
|
|
|
#else
|
2011-10-24 15:56:28 +02:00
|
|
|
static int set_managed_proxy_environment(char ***envp,
|
|
|
|
const managed_proxy_t *mp);
|
2011-10-17 22:46:44 +02:00
|
|
|
#endif
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
|
|
|
|
|
2011-10-24 15:59:11 +02:00
|
|
|
static void managed_proxy_destroy(managed_proxy_t *mp,
|
|
|
|
int also_terminate_process);
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
static void handle_finished_proxy(managed_proxy_t *mp);
|
|
|
|
static void configure_proxy(managed_proxy_t *mp);
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
static void parse_method_error(const char *line, int is_server_method);
|
2011-07-13 19:00:28 +02:00
|
|
|
#define parse_server_method_error(l) parse_method_error(l, 1)
|
|
|
|
#define parse_client_method_error(l) parse_method_error(l, 0)
|
|
|
|
|
|
|
|
static INLINE void free_execve_args(char **arg);
|
|
|
|
|
|
|
|
/** Managed proxy protocol strings */
|
|
|
|
#define PROTO_ENV_ERROR "ENV-ERROR"
|
|
|
|
#define PROTO_NEG_SUCCESS "VERSION"
|
|
|
|
#define PROTO_NEG_FAIL "VERSION-ERROR no-version"
|
|
|
|
#define PROTO_CMETHOD "CMETHOD"
|
|
|
|
#define PROTO_SMETHOD "SMETHOD"
|
|
|
|
#define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
|
|
|
|
#define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
|
|
|
|
#define PROTO_CMETHODS_DONE "CMETHODS DONE"
|
|
|
|
#define PROTO_SMETHODS_DONE "SMETHODS DONE"
|
|
|
|
|
|
|
|
/* The smallest valid managed proxy protocol line that can
|
|
|
|
appear. It's the size of "VERSION 1" */
|
|
|
|
#define SMALLEST_MANAGED_LINE_SIZE 9
|
|
|
|
|
|
|
|
/** Number of environment variables for managed proxy clients/servers. */
|
|
|
|
#define ENVIRON_SIZE_CLIENT 5
|
|
|
|
#define ENVIRON_SIZE_SERVER 8
|
|
|
|
|
|
|
|
/** The first and only supported - at the moment - configuration
|
|
|
|
protocol version. */
|
|
|
|
#define PROTO_VERSION_ONE 1
|
|
|
|
|
|
|
|
/** List of unconfigured managed proxies. */
|
2011-09-11 20:29:12 +02:00
|
|
|
static smartlist_t *managed_proxy_list = NULL;
|
|
|
|
/** Number of still unconfigured proxies. */
|
|
|
|
static int unconfigured_proxies_n = 0;
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
/** Return true if there are still unconfigured managed proxies. */
|
|
|
|
int
|
|
|
|
pt_proxies_configuration_pending(void)
|
|
|
|
{
|
2011-09-11 20:29:12 +02:00
|
|
|
return !! unconfigured_proxies_n;
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
2011-08-12 21:33:05 +02:00
|
|
|
/** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
|
|
|
|
static int
|
2011-09-11 23:35:00 +02:00
|
|
|
managed_proxy_has_argv(const managed_proxy_t *mp, char **proxy_argv)
|
2011-08-12 21:33:05 +02:00
|
|
|
{
|
|
|
|
char **tmp1=proxy_argv;
|
|
|
|
char **tmp2=mp->argv;
|
|
|
|
|
|
|
|
tor_assert(tmp1);
|
|
|
|
tor_assert(tmp2);
|
|
|
|
|
|
|
|
while (*tmp1 && *tmp2) {
|
|
|
|
if (strcmp(*tmp1++, *tmp2++))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*tmp1 && !*tmp2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a managed proxy with the same argv as <b>proxy_argv</b>.
|
|
|
|
* If no such managed proxy exists, return NULL. */
|
|
|
|
static managed_proxy_t *
|
2011-09-11 20:30:08 +02:00
|
|
|
get_managed_proxy_by_argv_and_type(char **proxy_argv, int is_server)
|
2011-08-12 21:33:05 +02:00
|
|
|
{
|
2011-09-11 20:29:12 +02:00
|
|
|
if (!managed_proxy_list)
|
2011-08-12 21:33:05 +02:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
|
2011-09-11 20:30:08 +02:00
|
|
|
if (managed_proxy_has_argv(mp, proxy_argv) &&
|
|
|
|
mp->is_server == is_server)
|
2011-08-12 21:33:05 +02:00
|
|
|
return mp;
|
|
|
|
} SMARTLIST_FOREACH_END(mp);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add <b>transport</b> to managed proxy <b>mp</b>. */
|
|
|
|
static void
|
2011-08-15 17:26:03 +02:00
|
|
|
add_transport_to_proxy(const char *transport, managed_proxy_t *mp)
|
2011-08-12 21:33:05 +02:00
|
|
|
{
|
|
|
|
tor_assert(mp->transports_to_launch);
|
|
|
|
if (!smartlist_string_isin(mp->transports_to_launch, transport))
|
|
|
|
smartlist_add(mp->transports_to_launch, tor_strdup(transport));
|
|
|
|
}
|
|
|
|
|
2011-09-23 17:50:56 +02:00
|
|
|
/** Called when a SIGHUP occurs. Returns true if managed proxy
|
|
|
|
* <b>mp</b> needs to be restarted after the SIGHUP, based on the new
|
|
|
|
* torrc. */
|
2011-09-11 20:29:12 +02:00
|
|
|
static int
|
2011-09-11 23:35:00 +02:00
|
|
|
proxy_needs_restart(const managed_proxy_t *mp)
|
2011-09-11 20:29:12 +02:00
|
|
|
{
|
|
|
|
/* mp->transport_to_launch is populated with the names of the
|
|
|
|
transports that must be launched *after* the SIGHUP.
|
2011-09-23 17:50:56 +02:00
|
|
|
mp->transports is populated with the names of the transports that
|
|
|
|
were launched *before* the SIGHUP.
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
If the two lists contain the same strings, we don't need to
|
|
|
|
restart the proxy, since it already does what we want. */
|
|
|
|
|
|
|
|
tor_assert(smartlist_len(mp->transports_to_launch) > 0);
|
|
|
|
tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
|
|
|
|
|
|
|
|
if (smartlist_len(mp->transports_to_launch) != smartlist_len(mp->transports))
|
|
|
|
goto needs_restart;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t_t_l) {
|
|
|
|
if (!smartlist_string_isin(mp->transports, t_t_l))
|
|
|
|
goto needs_restart;
|
|
|
|
|
|
|
|
} SMARTLIST_FOREACH_END(t_t_l);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
needs_restart:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Managed proxy <b>mp</b> must be restarted. Do all the necessary
|
2011-09-23 17:50:56 +02:00
|
|
|
* preparations and then flag its state so that it will be relaunched
|
2011-09-11 20:29:12 +02:00
|
|
|
* in the next tick. */
|
|
|
|
static void
|
|
|
|
proxy_prepare_for_restart(managed_proxy_t *mp)
|
|
|
|
{
|
|
|
|
transport_t *t_tmp = NULL;
|
|
|
|
|
|
|
|
tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
/* destroy the process handle and terminate the process. */
|
2011-10-24 16:04:31 +02:00
|
|
|
tor_process_handle_destroy(mp->process_handle, 1);
|
2011-11-25 22:47:25 +01:00
|
|
|
mp->process_handle = NULL;
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
/* destroy all its old transports. we no longer use them. */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
|
|
|
|
t_tmp = transport_get_by_name(t_name);
|
|
|
|
if (t_tmp)
|
|
|
|
t_tmp->marked_for_removal = 1;
|
|
|
|
} SMARTLIST_FOREACH_END(t_name);
|
|
|
|
sweep_transport_list();
|
|
|
|
|
|
|
|
/* free the transport names in mp->transports */
|
|
|
|
SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
|
|
|
|
smartlist_clear(mp->transports);
|
|
|
|
|
|
|
|
/* flag it as an infant proxy so that it gets launched on next tick */
|
|
|
|
mp->conf_state = PT_PROTO_INFANT;
|
|
|
|
}
|
|
|
|
|
2011-08-12 21:33:05 +02:00
|
|
|
/** Launch managed proxy <b>mp</b>. */
|
|
|
|
static int
|
|
|
|
launch_managed_proxy(managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
2011-10-24 16:01:24 +02:00
|
|
|
int retval;
|
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
LPVOID envp=NULL;
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
set_managed_proxy_environment(&envp, mp);
|
2011-10-24 15:56:28 +02:00
|
|
|
tor_assert(envp);
|
2011-08-12 21:33:05 +02:00
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
/* Passing NULL as lpApplicationName makes Windows search for the .exe */
|
2011-10-24 16:01:24 +02:00
|
|
|
retval = tor_spawn_background(NULL, (const char **)mp->argv, envp,
|
2011-11-25 22:47:25 +01:00
|
|
|
&mp->process_handle);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
tor_free(envp);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
char **envp=NULL;
|
|
|
|
|
|
|
|
/* prepare the environment variables for the managed proxy */
|
2011-10-24 15:56:28 +02:00
|
|
|
if (set_managed_proxy_environment(&envp, mp) < 0) {
|
|
|
|
log_warn(LD_GENERAL, "Could not setup the environment of "
|
2011-10-24 16:01:24 +02:00
|
|
|
"the managed proxy at '%s'.", mp->argv[0]);
|
2011-10-24 15:56:28 +02:00
|
|
|
free_execve_args(envp);
|
2011-07-13 19:00:28 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
retval = tor_spawn_background(mp->argv[0], (const char **)mp->argv,
|
2011-11-25 22:47:25 +01:00
|
|
|
(const char **)envp, &mp->process_handle);
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
/* free the memory allocated by set_managed_proxy_environment(). */
|
2011-07-13 19:00:28 +02:00
|
|
|
free_execve_args(envp);
|
|
|
|
|
2011-10-07 15:44:44 +02:00
|
|
|
#endif
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
if (retval == PROCESS_STATUS_ERROR) {
|
2011-10-17 22:46:44 +02:00
|
|
|
log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
|
|
|
|
mp->argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
log_info(LD_CONFIG, "Managed proxy at '%s' has spawned with PID '%d'.",
|
2011-11-02 14:23:41 +01:00
|
|
|
mp->argv[0], tor_process_get_pid(mp->process_handle));
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-08-12 21:33:05 +02:00
|
|
|
mp->conf_state = PT_PROTO_LAUNCHED;
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if any of the managed proxies we are currently trying to
|
|
|
|
* configure have anything new to say. This is called from
|
|
|
|
* run_scheduled_events(). */
|
|
|
|
void
|
|
|
|
pt_configure_remaining_proxies(void)
|
|
|
|
{
|
2011-09-12 00:10:07 +02:00
|
|
|
log_debug(LD_CONFIG, "Configuring remaining managed proxies (%d)!",
|
|
|
|
unconfigured_proxies_n);
|
2011-09-11 20:29:12 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
|
2011-10-24 15:59:11 +02:00
|
|
|
tor_assert(mp->conf_state != PT_PROTO_BROKEN ||
|
|
|
|
mp->conf_state != PT_PROTO_FAILED_LAUNCH);
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
if (mp->got_hup) {
|
|
|
|
mp->got_hup = 0;
|
|
|
|
|
|
|
|
/* This proxy is marked by a SIGHUP. Check whether we need to
|
|
|
|
restart it. */
|
|
|
|
if (proxy_needs_restart(mp)) {
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_GENERAL, "Preparing managed proxy for restart.");
|
2011-09-11 20:29:12 +02:00
|
|
|
proxy_prepare_for_restart(mp);
|
|
|
|
continue;
|
|
|
|
} else { /* it doesn't need to be restarted. */
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_GENERAL, "Nothing changed for managed proxy after HUP: "
|
|
|
|
"not restarting.");
|
2011-09-11 20:29:12 +02:00
|
|
|
unconfigured_proxies_n--;
|
|
|
|
tor_assert(unconfigured_proxies_n >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
/* If the proxy is not fully configured, try to configure it
|
|
|
|
futher. */
|
|
|
|
if (!proxy_configuration_finished(mp))
|
|
|
|
configure_proxy(mp);
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
} SMARTLIST_FOREACH_END(mp);
|
|
|
|
}
|
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
|
|
|
|
/** Attempt to continue configuring managed proxy <b>mp</b>. */
|
|
|
|
static void
|
|
|
|
configure_proxy(managed_proxy_t *mp)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
char stdout_buf[200];
|
|
|
|
smartlist_t *lines = NULL;
|
|
|
|
|
|
|
|
/* if we haven't launched the proxy yet, do it now */
|
|
|
|
if (mp->conf_state == PT_PROTO_INFANT) {
|
|
|
|
if (launch_managed_proxy(mp) < 0) { /* launch fail */
|
2011-10-24 15:59:11 +02:00
|
|
|
mp->conf_state = PT_PROTO_FAILED_LAUNCH;
|
2011-10-17 22:46:44 +02:00
|
|
|
handle_finished_proxy(mp);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(mp->conf_state != PT_PROTO_INFANT);
|
2011-11-25 22:47:25 +01:00
|
|
|
tor_assert(mp->process_handle);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-11-25 22:47:25 +01:00
|
|
|
pos = tor_read_all_handle(tor_process_get_stdout_pipe(mp->process_handle),
|
2011-10-17 22:46:44 +02:00
|
|
|
stdout_buf, sizeof(stdout_buf) - 1, NULL);
|
|
|
|
if (pos < 0) {
|
|
|
|
log_notice(LD_GENERAL, "Failed to read data from managed proxy");
|
|
|
|
mp->conf_state = PT_PROTO_BROKEN;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == 0) /* proxy has nothing interesting to say. */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* End with a null even if there isn't a \r\n at the end */
|
|
|
|
/* TODO: What if this is a partial line? */
|
|
|
|
stdout_buf[pos] = '\0';
|
|
|
|
|
|
|
|
/* Split up the buffer */
|
2012-01-18 21:53:30 +01:00
|
|
|
lines = smartlist_new();
|
2011-10-17 22:46:44 +02:00
|
|
|
tor_split_lines(lines, stdout_buf, pos);
|
|
|
|
|
|
|
|
/* Handle lines. */
|
2011-10-24 16:01:24 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(lines, const char *, line) {
|
|
|
|
handle_proxy_line(line, mp);
|
|
|
|
if (proxy_configuration_finished(mp))
|
|
|
|
goto done;
|
|
|
|
} SMARTLIST_FOREACH_END(line);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
/* if the proxy finished configuring, exit the loop. */
|
|
|
|
if (proxy_configuration_finished(mp))
|
|
|
|
handle_finished_proxy(mp);
|
|
|
|
|
|
|
|
if (lines)
|
|
|
|
smartlist_free(lines);
|
|
|
|
}
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
#else /* MS_WINDOWS */
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-08-12 21:33:05 +02:00
|
|
|
/** Attempt to continue configuring managed proxy <b>mp</b>. */
|
2011-07-13 19:00:28 +02:00
|
|
|
static void
|
|
|
|
configure_proxy(managed_proxy_t *mp)
|
|
|
|
{
|
|
|
|
enum stream_status r;
|
|
|
|
char stdout_buf[200];
|
|
|
|
|
2011-08-12 21:33:05 +02:00
|
|
|
/* if we haven't launched the proxy yet, do it now */
|
|
|
|
if (mp->conf_state == PT_PROTO_INFANT) {
|
2011-10-17 22:46:44 +02:00
|
|
|
if (launch_managed_proxy(mp) < 0) { /* launch fail */
|
2011-10-24 15:59:11 +02:00
|
|
|
mp->conf_state = PT_PROTO_FAILED_LAUNCH;
|
2011-10-17 22:46:44 +02:00
|
|
|
handle_finished_proxy(mp);
|
|
|
|
}
|
2011-08-12 21:33:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(mp->conf_state != PT_PROTO_INFANT);
|
2011-11-25 22:47:25 +01:00
|
|
|
tor_assert(mp->process_handle);
|
2011-08-12 21:33:05 +02:00
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
while (1) {
|
2011-11-25 22:47:25 +01:00
|
|
|
r = get_string_from_pipe(tor_process_get_stdout_pipe(mp->process_handle),
|
2011-10-17 22:46:44 +02:00
|
|
|
stdout_buf, sizeof(stdout_buf) - 1);
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
|
|
|
|
handle_proxy_line((const char *)stdout_buf, mp);
|
|
|
|
} else if (r == IO_STREAM_EAGAIN) { /* check back later */
|
|
|
|
return;
|
|
|
|
} else if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) { /* snap! */
|
2011-09-12 00:10:07 +02:00
|
|
|
log_notice(LD_GENERAL, "Managed proxy stream closed. "
|
|
|
|
"Most probably application stopped running");
|
2011-07-13 19:00:28 +02:00
|
|
|
mp->conf_state = PT_PROTO_BROKEN;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else { /* unknown stream status */
|
2011-09-12 00:10:07 +02:00
|
|
|
log_notice(LD_GENERAL, "Unknown stream status while configuring proxy.");
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the proxy finished configuring, exit the loop. */
|
|
|
|
if (proxy_configuration_finished(mp)) {
|
|
|
|
handle_finished_proxy(mp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
#endif /* MS_WINDOWS */
|
|
|
|
|
2011-08-07 18:05:40 +02:00
|
|
|
/** Register server managed proxy <b>mp</b> transports to state */
|
|
|
|
static void
|
2011-09-11 20:29:12 +02:00
|
|
|
register_server_proxy(managed_proxy_t *mp)
|
2011-08-07 18:05:40 +02:00
|
|
|
{
|
2011-09-23 17:50:56 +02:00
|
|
|
/* After we register this proxy's transports, we switch its
|
|
|
|
mp->transports to a list containing strings of its transport
|
|
|
|
names. (See transports.h) */
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *sm_tmp = smartlist_new();
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
|
2011-09-11 23:33:31 +02:00
|
|
|
save_transport_to_state(t->name, &t->addr, t->port);
|
2011-11-02 14:23:41 +01:00
|
|
|
/* LOG_WARN so that the bridge operator can easily find the
|
|
|
|
transport's port in the log file and send it to the users. */
|
|
|
|
log_warn(LD_GENERAL, "Registered server transport '%s' at '%s:%d'",
|
|
|
|
t->name, fmt_addr(&t->addr), (int)t->port);
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_add(sm_tmp, tor_strdup(t->name));
|
|
|
|
} SMARTLIST_FOREACH_END(t);
|
|
|
|
|
2011-09-11 23:33:31 +02:00
|
|
|
/* Since server proxies don't register their transports in the
|
|
|
|
circuitbuild.c subsystem, it's our duty to free them when we
|
|
|
|
switch mp->transports to strings. */
|
|
|
|
SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_free(mp->transports);
|
2011-09-11 23:33:31 +02:00
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
mp->transports = sm_tmp;
|
2011-08-07 18:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Register all the transports supported by client managed proxy
|
|
|
|
* <b>mp</b> to the bridge subsystem. */
|
|
|
|
static void
|
2011-09-11 20:29:12 +02:00
|
|
|
register_client_proxy(managed_proxy_t *mp)
|
2011-08-07 18:05:40 +02:00
|
|
|
{
|
2011-09-11 20:29:12 +02:00
|
|
|
int r;
|
2011-09-23 17:50:56 +02:00
|
|
|
/* After we register this proxy's transports, we switch its
|
|
|
|
mp->transports to a list containing strings of its transport
|
|
|
|
names. (See transports.h) */
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *sm_tmp = smartlist_new();
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
|
2011-08-07 18:05:40 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
|
2011-09-11 20:29:12 +02:00
|
|
|
r = transport_add(t);
|
|
|
|
switch (r) {
|
|
|
|
case -1:
|
2011-09-12 00:10:07 +02:00
|
|
|
log_notice(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
|
2011-08-07 18:05:40 +02:00
|
|
|
transport_free(t);
|
2011-09-11 20:29:12 +02:00
|
|
|
break;
|
|
|
|
case 0:
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_add(sm_tmp, tor_strdup(t->name));
|
|
|
|
break;
|
|
|
|
case 1:
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_add(sm_tmp, tor_strdup(t->name));
|
|
|
|
transport_free(t);
|
|
|
|
break;
|
2011-08-07 18:05:40 +02:00
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(t);
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
smartlist_free(mp->transports);
|
|
|
|
mp->transports = sm_tmp;
|
2011-08-07 18:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Register the transports of managed proxy <b>mp</b>. */
|
|
|
|
static INLINE void
|
2011-09-11 20:29:12 +02:00
|
|
|
register_proxy(managed_proxy_t *mp)
|
2011-08-07 18:05:40 +02:00
|
|
|
{
|
|
|
|
if (mp->is_server)
|
|
|
|
register_server_proxy(mp);
|
|
|
|
else
|
|
|
|
register_client_proxy(mp);
|
|
|
|
}
|
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
/** Free memory allocated by managed proxy <b>mp</b>. */
|
2011-07-13 19:00:28 +02:00
|
|
|
static void
|
2011-10-24 15:59:11 +02:00
|
|
|
managed_proxy_destroy(managed_proxy_t *mp,
|
|
|
|
int also_terminate_process)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
2011-09-11 20:29:12 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_COMPLETED)
|
2011-07-13 19:00:28 +02:00
|
|
|
SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
|
2011-09-11 20:29:12 +02:00
|
|
|
else
|
|
|
|
SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
/* free the transports smartlist */
|
|
|
|
smartlist_free(mp->transports);
|
|
|
|
|
2011-09-23 17:50:56 +02:00
|
|
|
/* free the transports_to_launch smartlist */
|
2011-08-12 21:33:05 +02:00
|
|
|
SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
|
|
|
|
smartlist_free(mp->transports_to_launch);
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
/* remove it from the list of managed proxies */
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_remove(managed_proxy_list, mp);
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-08-12 21:33:05 +02:00
|
|
|
/* free the argv */
|
|
|
|
free_execve_args(mp->argv);
|
|
|
|
|
2011-10-24 16:04:31 +02:00
|
|
|
tor_process_handle_destroy(mp->process_handle, also_terminate_process);
|
2011-11-25 22:47:25 +01:00
|
|
|
mp->process_handle = NULL;
|
2011-09-11 20:29:12 +02:00
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
tor_free(mp);
|
|
|
|
}
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
/** Handle a configured or broken managed proxy <b>mp</b>. */
|
|
|
|
static void
|
|
|
|
handle_finished_proxy(managed_proxy_t *mp)
|
|
|
|
{
|
|
|
|
switch (mp->conf_state) {
|
|
|
|
case PT_PROTO_BROKEN: /* if broken: */
|
2011-10-24 15:59:11 +02:00
|
|
|
managed_proxy_destroy(mp, 1); /* annihilate it. */
|
|
|
|
break;
|
2011-10-24 16:01:24 +02:00
|
|
|
case PT_PROTO_FAILED_LAUNCH: /* if it failed before launching: */
|
|
|
|
managed_proxy_destroy(mp, 0); /* destroy it but don't terminate */
|
2011-08-15 17:26:03 +02:00
|
|
|
break;
|
|
|
|
case PT_PROTO_CONFIGURED: /* if configured correctly: */
|
2011-09-23 17:50:56 +02:00
|
|
|
register_proxy(mp); /* register its transports */
|
|
|
|
mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
|
2011-08-15 17:26:03 +02:00
|
|
|
break;
|
2011-09-11 23:33:31 +02:00
|
|
|
case PT_PROTO_INFANT:
|
|
|
|
case PT_PROTO_LAUNCHED:
|
|
|
|
case PT_PROTO_ACCEPTING_METHODS:
|
|
|
|
case PT_PROTO_COMPLETED:
|
2011-08-15 17:26:03 +02:00
|
|
|
default:
|
2011-09-11 23:33:31 +02:00
|
|
|
log_warn(LD_CONFIG, "Unexpected managed proxy state in "
|
2011-08-15 17:26:03 +02:00
|
|
|
"handle_finished_proxy().");
|
2011-09-10 00:45:28 +02:00
|
|
|
tor_assert(0);
|
2011-08-15 17:26:03 +02:00
|
|
|
}
|
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
unconfigured_proxies_n--;
|
|
|
|
tor_assert(unconfigured_proxies_n >= 0);
|
2011-08-15 17:26:03 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
/** Return true if the configuration of the managed proxy <b>mp</b> is
|
|
|
|
finished. */
|
|
|
|
static INLINE int
|
2011-08-15 17:26:03 +02:00
|
|
|
proxy_configuration_finished(const managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
|
|
|
return (mp->conf_state == PT_PROTO_CONFIGURED ||
|
2011-10-24 15:59:11 +02:00
|
|
|
mp->conf_state == PT_PROTO_BROKEN ||
|
|
|
|
mp->conf_state == PT_PROTO_FAILED_LAUNCH);
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
/** This function is called when a proxy sends an {S,C}METHODS DONE message. */
|
2011-08-15 17:26:03 +02:00
|
|
|
static void
|
|
|
|
handle_methods_done(const managed_proxy_t *mp)
|
|
|
|
{
|
|
|
|
tor_assert(mp->transports);
|
|
|
|
|
|
|
|
if (smartlist_len(mp->transports) == 0)
|
2011-09-12 00:10:07 +02:00
|
|
|
log_notice(LD_GENERAL, "Proxy was spawned successfully, "
|
|
|
|
"but it didn't laucn any pluggable transport listeners!");
|
2011-08-15 17:26:03 +02:00
|
|
|
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_CONFIG, "%s managed proxy configuration completed!",
|
2011-08-15 17:26:03 +02:00
|
|
|
mp->is_server ? "Server" : "Client");
|
|
|
|
}
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
/** Handle a configuration protocol <b>line</b> received from a
|
|
|
|
* managed proxy <b>mp</b>. */
|
|
|
|
void
|
2011-08-15 17:26:03 +02:00
|
|
|
handle_proxy_line(const char *line, managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
2011-10-24 15:56:28 +02:00
|
|
|
log_debug(LD_GENERAL, "Got a line from managed proxy: %s", line);
|
2011-08-12 21:33:05 +02:00
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
|
|
|
|
log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
|
|
|
|
"Discarding");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
if (!strcmpstart(line, PROTO_ENV_ERROR)) {
|
2011-08-12 21:33:05 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_LAUNCHED)
|
2011-07-13 19:00:28 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
parse_env_error(line);
|
|
|
|
goto err;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
|
2011-08-12 21:33:05 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_LAUNCHED)
|
2011-07-13 19:00:28 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
log_warn(LD_CONFIG, "Managed proxy could not pick a "
|
|
|
|
"configuration protocol version.");
|
|
|
|
goto err;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
|
2011-08-12 21:33:05 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_LAUNCHED)
|
2011-07-13 19:00:28 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (parse_version(line,mp) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
tor_assert(mp->conf_protocol != 0);
|
|
|
|
mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
|
|
|
|
return;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
|
2011-07-13 19:00:28 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
|
|
|
|
goto err;
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
handle_methods_done(mp);
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
mp->conf_state = PT_PROTO_CONFIGURED;
|
|
|
|
return;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
|
2011-07-13 19:00:28 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
|
|
|
|
goto err;
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
handle_methods_done(mp);
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
mp->conf_state = PT_PROTO_CONFIGURED;
|
|
|
|
return;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
|
2011-07-13 19:00:28 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
parse_client_method_error(line);
|
|
|
|
goto err;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
|
2011-07-13 19:00:28 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
parse_server_method_error(line);
|
|
|
|
goto err;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_CMETHOD)) {
|
2011-07-13 19:00:28 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (parse_cmethod_line(line, mp) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, PROTO_SMETHOD)) {
|
2011-07-13 19:00:28 +02:00
|
|
|
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (parse_smethod_line(line, mp) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return;
|
2011-08-15 17:26:03 +02:00
|
|
|
} else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
|
|
|
|
log_warn(LD_GENERAL, "Could not launch managed proxy executable!");
|
2011-10-24 15:59:11 +02:00
|
|
|
mp->conf_state = PT_PROTO_FAILED_LAUNCH;
|
|
|
|
return;
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
|
|
|
|
|
|
|
|
err:
|
|
|
|
mp->conf_state = PT_PROTO_BROKEN;
|
2011-10-24 16:01:24 +02:00
|
|
|
log_warn(LD_CONFIG, "Managed proxy at '%s' failed the configuration protocol"
|
2011-11-25 22:54:06 +01:00
|
|
|
" and will be destroyed.", mp->argv ? mp->argv[0] : "");
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
|
|
|
|
void
|
2011-08-15 17:26:03 +02:00
|
|
|
parse_env_error(const char *line)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
|
|
|
/* (Length of the protocol string) plus (a space) and (the first char of
|
|
|
|
the error message) */
|
|
|
|
if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
|
2011-09-12 00:10:07 +02:00
|
|
|
log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
|
|
|
|
"message.", PROTO_ENV_ERROR);
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
|
|
|
|
"pluggable transport environment variables. (%s)",
|
|
|
|
line+strlen(PROTO_ENV_ERROR)+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Handles a VERSION <b>line</b>. Updates the configuration protocol
|
|
|
|
* version in <b>mp</b>. */
|
|
|
|
int
|
2011-08-15 17:26:03 +02:00
|
|
|
parse_version(const char *line, managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
|
|
|
if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
|
|
|
|
log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
|
|
|
|
PROTO_NEG_SUCCESS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-12 00:10:07 +02:00
|
|
|
if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) { /* hardcoded temp */
|
|
|
|
log_warn(LD_CONFIG, "Managed proxy tried to negotiate on version '%s'. "
|
2011-07-13 19:00:28 +02:00
|
|
|
"We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
|
|
|
|
* accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
|
|
|
|
* otherwise it is a CMETHOD-ERROR. */
|
|
|
|
static void
|
2011-08-15 17:26:03 +02:00
|
|
|
parse_method_error(const char *line, int is_server)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
|
|
|
const char* error = is_server ?
|
|
|
|
PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
|
|
|
|
|
|
|
|
/* (Length of the protocol string) plus (a space) and (the first char of
|
|
|
|
the error message) */
|
|
|
|
if (strlen(line) < (strlen(error) + 2))
|
|
|
|
log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
|
|
|
|
"message.", error);
|
|
|
|
|
|
|
|
log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
|
|
|
|
is_server ? "Server" : "Client",
|
|
|
|
line+strlen(error)+1);
|
|
|
|
}
|
|
|
|
|
2011-08-07 18:05:40 +02:00
|
|
|
/** Parses an SMETHOD <b>line</b> and if well-formed it registers the
|
|
|
|
* new transport in <b>mp</b>. */
|
2011-07-13 19:00:28 +02:00
|
|
|
int
|
2011-08-15 17:26:03 +02:00
|
|
|
parse_smethod_line(const char *line, managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
smartlist_t *items = NULL;
|
|
|
|
|
|
|
|
char *method_name=NULL;
|
|
|
|
|
|
|
|
char *addrport=NULL;
|
|
|
|
tor_addr_t addr;
|
|
|
|
uint16_t port = 0;
|
|
|
|
|
2011-08-07 18:05:40 +02:00
|
|
|
transport_t *transport=NULL;
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
items = smartlist_new();
|
2011-07-13 19:00:28 +02:00
|
|
|
smartlist_split_string(items, line, NULL,
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
|
|
|
|
if (smartlist_len(items) < 3) {
|
|
|
|
log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
|
|
|
|
"with too few arguments.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
|
|
|
|
|
|
|
|
method_name = smartlist_get(items,1);
|
2011-09-11 23:34:11 +02:00
|
|
|
if (!string_is_C_identifier(method_name)) {
|
|
|
|
log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
|
|
|
|
method_name);
|
|
|
|
goto err;
|
|
|
|
}
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
addrport = smartlist_get(items, 2);
|
2011-10-11 17:21:31 +02:00
|
|
|
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
|
2011-07-13 19:00:28 +02:00
|
|
|
log_warn(LD_CONFIG, "Error parsing transport "
|
|
|
|
"address '%s'", addrport);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!port) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Transport address '%s' has no port.", addrport);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
transport = transport_new(&addr, port, method_name, PROXY_NONE);
|
2011-08-07 18:05:40 +02:00
|
|
|
if (!transport)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
smartlist_add(mp->transports, transport);
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
/* For now, notify the user so that he knows where the server
|
|
|
|
transport is listening. */
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_CONFIG, "Server transport %s at %s:%d.",
|
2011-07-13 19:00:28 +02:00
|
|
|
method_name, fmt_addr(&addr), (int)port);
|
|
|
|
|
|
|
|
r=0;
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
r = -1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
|
|
|
|
smartlist_free(items);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parses a CMETHOD <b>line</b>, and if well-formed it registers
|
|
|
|
* the new transport in <b>mp</b>. */
|
|
|
|
int
|
2011-08-15 17:26:03 +02:00
|
|
|
parse_cmethod_line(const char *line, managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
smartlist_t *items = NULL;
|
|
|
|
|
|
|
|
char *method_name=NULL;
|
|
|
|
|
|
|
|
char *socks_ver_str=NULL;
|
|
|
|
int socks_ver=PROXY_NONE;
|
|
|
|
|
|
|
|
char *addrport=NULL;
|
|
|
|
tor_addr_t addr;
|
|
|
|
uint16_t port = 0;
|
|
|
|
|
|
|
|
transport_t *transport=NULL;
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
items = smartlist_new();
|
2011-07-13 19:00:28 +02:00
|
|
|
smartlist_split_string(items, line, NULL,
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
|
|
|
|
if (smartlist_len(items) < 4) {
|
|
|
|
log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
|
|
|
|
"with too few arguments.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
|
|
|
|
|
|
|
|
method_name = smartlist_get(items,1);
|
2011-09-11 23:34:11 +02:00
|
|
|
if (!string_is_C_identifier(method_name)) {
|
|
|
|
log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
|
|
|
|
method_name);
|
|
|
|
goto err;
|
|
|
|
}
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
socks_ver_str = smartlist_get(items,2);
|
|
|
|
|
|
|
|
if (!strcmp(socks_ver_str,"socks4")) {
|
|
|
|
socks_ver = PROXY_SOCKS4;
|
|
|
|
} else if (!strcmp(socks_ver_str,"socks5")) {
|
|
|
|
socks_ver = PROXY_SOCKS5;
|
|
|
|
} else {
|
|
|
|
log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
|
|
|
|
"we don't recognize. (%s)", socks_ver_str);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrport = smartlist_get(items, 3);
|
2011-10-11 17:21:31 +02:00
|
|
|
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
|
2011-07-13 19:00:28 +02:00
|
|
|
log_warn(LD_CONFIG, "Error parsing transport "
|
|
|
|
"address '%s'", addrport);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!port) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Transport address '%s' has no port.", addrport);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
transport = transport_new(&addr, port, method_name, socks_ver);
|
2011-07-13 19:00:28 +02:00
|
|
|
if (!transport)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
smartlist_add(mp->transports, transport);
|
|
|
|
|
2011-09-12 00:10:07 +02:00
|
|
|
log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
|
2011-07-13 19:00:28 +02:00
|
|
|
"Attached to managed proxy.",
|
|
|
|
method_name, fmt_addr(&addr), (int)port, socks_ver);
|
|
|
|
|
|
|
|
r=0;
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
r = -1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
|
|
|
|
smartlist_free(items);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-13 15:44:30 +01:00
|
|
|
/** Return the string that tor should place in TOR_PT_SERVER_BINDADDR
|
|
|
|
* while configuring the server managed proxy in <b>mp</b>. The
|
|
|
|
* string is stored in the heap, and it's the the responsibility of
|
|
|
|
* the caller to deallocate it after its use. */
|
2011-08-12 21:33:05 +02:00
|
|
|
static char *
|
2012-01-13 15:44:30 +01:00
|
|
|
get_bindaddr_for_server_proxy(const managed_proxy_t *mp)
|
2011-08-12 21:33:05 +02:00
|
|
|
{
|
2012-01-11 19:44:10 +01:00
|
|
|
char *bindaddr_result = NULL;
|
2011-12-18 13:21:58 +01:00
|
|
|
char *bindaddr_tmp = NULL;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *string_tmp = smartlist_new();
|
2011-08-12 21:33:05 +02:00
|
|
|
|
|
|
|
tor_assert(mp->is_server);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
|
2012-01-13 15:44:30 +01:00
|
|
|
bindaddr_tmp = get_stored_bindaddr_for_server_transport(t);
|
2011-12-18 13:21:58 +01:00
|
|
|
|
2012-01-11 19:44:10 +01:00
|
|
|
smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);
|
2011-12-18 13:21:58 +01:00
|
|
|
|
|
|
|
tor_free(bindaddr_tmp);
|
2011-08-12 21:33:05 +02:00
|
|
|
} SMARTLIST_FOREACH_END(t);
|
|
|
|
|
2012-01-11 19:44:10 +01:00
|
|
|
bindaddr_result = smartlist_join_strings(string_tmp, ",", 0, NULL);
|
2011-08-12 21:33:05 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
|
|
|
|
smartlist_free(string_tmp);
|
|
|
|
|
2012-01-11 19:44:10 +01:00
|
|
|
return bindaddr_result;
|
2011-08-12 21:33:05 +02:00
|
|
|
}
|
|
|
|
|
2011-10-17 22:46:44 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2011-10-24 15:56:28 +02:00
|
|
|
|
|
|
|
/** Prepare the environment <b>envp</b> of managed proxy <b>mp</b>.
|
|
|
|
* <b>envp</b> is allocated on the heap and should be freed by the
|
|
|
|
* caller after its use. */
|
2011-07-13 19:00:28 +02:00
|
|
|
static void
|
2011-10-24 15:56:28 +02:00
|
|
|
set_managed_proxy_environment(LPVOID *envp, const managed_proxy_t *mp)
|
2011-10-17 22:46:44 +02:00
|
|
|
{
|
|
|
|
const or_options_t *options = get_options();
|
2011-10-24 15:56:28 +02:00
|
|
|
|
2011-12-01 09:40:47 +01:00
|
|
|
char *tmp=NULL;
|
2011-10-24 15:56:28 +02:00
|
|
|
char *state_tmp=NULL;
|
|
|
|
char *state_env=NULL;
|
2011-10-17 22:46:44 +02:00
|
|
|
char *transports_to_launch=NULL;
|
2011-10-24 15:56:28 +02:00
|
|
|
char *transports_env=NULL;
|
|
|
|
char *bindaddr_tmp=NULL;
|
|
|
|
char *bindaddr_env=NULL;
|
|
|
|
char *orport_env=NULL;
|
|
|
|
|
|
|
|
char version_env[31]; /* XXX temp */
|
|
|
|
char extended_env[43]; /* XXX temp */
|
|
|
|
|
|
|
|
int env_size = 0;
|
|
|
|
|
|
|
|
/* A smartlist carrying all the env. variables that the managed
|
|
|
|
proxy should inherit. */
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *envs = smartlist_new();
|
2011-10-24 15:56:28 +02:00
|
|
|
|
|
|
|
/* Copy the whole environment of the Tor process.
|
|
|
|
It should also copy PATH and HOME of the Tor process.*/
|
|
|
|
char **environ_tmp = environ;
|
|
|
|
while (*environ_tmp)
|
|
|
|
smartlist_add(envs, *environ_tmp++);
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
/* Create the TOR_PT_* environment variables. */
|
2011-10-24 15:56:28 +02:00
|
|
|
state_tmp = get_datadir_fname("pt_state/"); /* XXX temp */
|
|
|
|
tor_asprintf(&state_env, "TOR_PT_STATE_LOCATION=%s", state_tmp);
|
|
|
|
|
|
|
|
strcpy(version_env, "TOR_PT_MANAGED_TRANSPORT_VER=1");
|
2011-10-17 22:46:44 +02:00
|
|
|
|
|
|
|
transports_to_launch =
|
|
|
|
smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
|
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
tor_asprintf(&transports_env,
|
|
|
|
mp->is_server ?
|
|
|
|
"TOR_PT_SERVER_TRANSPORTS=%s" : "TOR_PT_CLIENT_TRANSPORTS=%s",
|
|
|
|
transports_to_launch);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
smartlist_add(envs, state_env);
|
|
|
|
smartlist_add(envs, version_env);
|
|
|
|
smartlist_add(envs, transports_env);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
|
|
|
if (mp->is_server) {
|
2011-12-01 09:40:47 +01:00
|
|
|
tor_asprintf(&orport_env, "TOR_PT_ORPORT=127.0.0.1:%s",
|
|
|
|
options->ORPort->value);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2012-01-13 15:44:30 +01:00
|
|
|
bindaddr_tmp = get_bindaddr_for_server_proxy(mp);
|
2011-10-24 15:56:28 +02:00
|
|
|
tor_asprintf(&bindaddr_env, "TOR_PT_SERVER_BINDADDR=%s", bindaddr_tmp);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
strcpy(extended_env, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200");
|
|
|
|
|
|
|
|
smartlist_add(envs, orport_env);
|
|
|
|
smartlist_add(envs, extended_env);
|
|
|
|
smartlist_add(envs, bindaddr_env);
|
2011-10-17 22:46:44 +02:00
|
|
|
}
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
/* It seems like some versions of Windows need a sorted lpEnvironment
|
|
|
|
block. */
|
2011-10-24 15:56:28 +02:00
|
|
|
smartlist_sort_strings(envs);
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
/* An environment block consists of a null-terminated block of
|
2011-10-24 16:01:24 +02:00
|
|
|
null-terminated strings: */
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
/* Calculate the block's size. */
|
|
|
|
SMARTLIST_FOREACH(envs, const char *, s,
|
|
|
|
env_size += strlen(s) + 1);
|
|
|
|
env_size += 1; /* space for last NUL */
|
|
|
|
|
|
|
|
*envp = tor_malloc(env_size);
|
|
|
|
tmp = *envp;
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
/* Create the block. */
|
2011-10-24 15:56:28 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(envs, const char *, s) {
|
|
|
|
memcpy(tmp, s, strlen(s)); /* copy the env. variable string */
|
|
|
|
tmp += strlen(s);
|
|
|
|
memset(tmp, '\0', 1); /* append NUL at the end of the string */
|
|
|
|
tmp += 1;
|
|
|
|
} SMARTLIST_FOREACH_END(s);
|
|
|
|
memset(tmp, '\0', 1); /* last NUL */
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
/* Finally, free the whole mess. */
|
2011-10-24 15:56:28 +02:00
|
|
|
tor_free(state_tmp);
|
|
|
|
tor_free(state_env);
|
|
|
|
tor_free(transports_to_launch);
|
|
|
|
tor_free(transports_env);
|
|
|
|
tor_free(bindaddr_tmp);
|
|
|
|
tor_free(bindaddr_env);
|
|
|
|
tor_free(orport_env);
|
2011-10-24 16:01:24 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
smartlist_free(envs);
|
2011-10-17 22:46:44 +02:00
|
|
|
}
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
#else /* MS_WINDOWS */
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
/** Prepare the environment <b>envp</b> of managed proxy <b>mp</b>.
|
|
|
|
* <b>envp</b> is allocated on the heap and should be freed by the
|
|
|
|
* caller after its use. */
|
|
|
|
static int
|
2011-08-15 17:26:03 +02:00
|
|
|
set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
|
2011-07-13 19:00:28 +02:00
|
|
|
{
|
2011-10-07 22:05:13 +02:00
|
|
|
const or_options_t *options = get_options();
|
2011-07-13 19:00:28 +02:00
|
|
|
char **tmp=NULL;
|
|
|
|
char *state_loc=NULL;
|
2011-08-12 21:33:05 +02:00
|
|
|
char *transports_to_launch=NULL;
|
|
|
|
char *bindaddr=NULL;
|
2011-10-24 15:56:28 +02:00
|
|
|
char *home_env=NULL;
|
|
|
|
char *path_env=NULL;
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
int r = -1;
|
2011-08-12 21:33:05 +02:00
|
|
|
int n_envs = mp->is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
|
2011-07-13 19:00:28 +02:00
|
|
|
|
|
|
|
/* allocate enough space for our env. vars and a NULL pointer */
|
|
|
|
*envp = tor_malloc(sizeof(char*)*(n_envs+1));
|
|
|
|
tmp = *envp;
|
|
|
|
|
2011-08-15 17:26:03 +02:00
|
|
|
state_loc = get_datadir_fname("pt_state/"); /* XXX temp */
|
2011-08-12 21:33:05 +02:00
|
|
|
transports_to_launch =
|
|
|
|
smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
|
2011-08-07 18:05:40 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
home_env = getenv("HOME");
|
|
|
|
path_env = getenv("PATH");
|
|
|
|
if (!home_env || !path_env)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
tor_asprintf(tmp++, "HOME=%s", home_env);
|
|
|
|
tor_asprintf(tmp++, "PATH=%s", path_env);
|
2011-07-13 19:00:28 +02:00
|
|
|
tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
|
|
|
|
tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
|
2011-08-12 21:33:05 +02:00
|
|
|
if (mp->is_server) {
|
2012-01-13 15:44:30 +01:00
|
|
|
bindaddr = get_bindaddr_for_server_proxy(mp);
|
2011-08-12 21:33:05 +02:00
|
|
|
|
2011-09-11 23:34:36 +02:00
|
|
|
/* XXX temp */
|
2011-11-08 22:10:38 +01:00
|
|
|
tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d",
|
|
|
|
router_get_advertised_or_port(options));
|
2011-08-12 21:33:05 +02:00
|
|
|
tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
|
|
|
|
tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
|
2011-09-11 23:34:36 +02:00
|
|
|
/* XXX temp*/
|
|
|
|
tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200");
|
2011-07-13 19:00:28 +02:00
|
|
|
} else {
|
2011-08-12 21:33:05 +02:00
|
|
|
tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
*tmp = NULL;
|
2011-08-07 18:05:40 +02:00
|
|
|
|
2011-10-24 15:56:28 +02:00
|
|
|
r = 0;
|
|
|
|
|
|
|
|
done:
|
2011-08-07 18:05:40 +02:00
|
|
|
tor_free(state_loc);
|
2011-08-12 21:33:05 +02:00
|
|
|
tor_free(transports_to_launch);
|
|
|
|
tor_free(bindaddr);
|
2011-10-24 15:56:28 +02:00
|
|
|
|
|
|
|
return r;
|
2011-08-12 21:33:05 +02:00
|
|
|
}
|
|
|
|
|
2011-10-24 16:01:24 +02:00
|
|
|
#endif /* MS_WINDOWS */
|
2011-10-17 22:46:44 +02:00
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
/** Create and return a new managed proxy for <b>transport</b> using
|
|
|
|
* <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
|
|
|
|
* managed proxy. */
|
|
|
|
static managed_proxy_t *
|
2011-10-07 14:13:41 +02:00
|
|
|
managed_proxy_create(const smartlist_t *transport_list,
|
|
|
|
char **proxy_argv, int is_server)
|
2011-09-11 20:29:12 +02:00
|
|
|
{
|
|
|
|
managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
|
|
|
|
mp->conf_state = PT_PROTO_INFANT;
|
|
|
|
mp->is_server = is_server;
|
|
|
|
mp->argv = proxy_argv;
|
2012-01-18 21:53:30 +01:00
|
|
|
mp->transports = smartlist_new();
|
2011-09-11 20:29:12 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
mp->transports_to_launch = smartlist_new();
|
2011-10-07 14:13:41 +02:00
|
|
|
SMARTLIST_FOREACH(transport_list, const char *, transport,
|
|
|
|
add_transport_to_proxy(transport, mp));
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
/* register the managed proxy */
|
|
|
|
if (!managed_proxy_list)
|
2012-01-18 21:53:30 +01:00
|
|
|
managed_proxy_list = smartlist_new();
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_add(managed_proxy_list, mp);
|
|
|
|
unconfigured_proxies_n++;
|
|
|
|
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
2012-01-13 15:44:30 +01:00
|
|
|
/** Register proxy with <b>proxy_argv</b>, supporting transports in
|
|
|
|
* <b>transport_list</b>, to the managed proxy subsystem.
|
2011-08-12 21:33:05 +02:00
|
|
|
* If <b>is_server</b> is true, then the proxy is a server proxy. */
|
|
|
|
void
|
2011-10-07 14:13:41 +02:00
|
|
|
pt_kickstart_proxy(const smartlist_t *transport_list,
|
|
|
|
char **proxy_argv, int is_server)
|
2011-08-12 21:33:05 +02:00
|
|
|
{
|
|
|
|
managed_proxy_t *mp=NULL;
|
2011-09-11 23:33:31 +02:00
|
|
|
transport_t *old_transport = NULL;
|
2011-08-12 21:33:05 +02:00
|
|
|
|
2011-09-11 20:30:08 +02:00
|
|
|
mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
|
2011-08-12 21:33:05 +02:00
|
|
|
|
|
|
|
if (!mp) { /* we haven't seen this proxy before */
|
2011-10-07 14:13:41 +02:00
|
|
|
managed_proxy_create(transport_list, proxy_argv, is_server);
|
2011-09-11 20:29:12 +02:00
|
|
|
|
|
|
|
} else { /* known proxy. add its transport to its transport list */
|
|
|
|
if (mp->got_hup) {
|
|
|
|
/* If the managed proxy we found is marked by a SIGHUP, it means
|
|
|
|
that it's not useless and should be kept. If it's marked for
|
|
|
|
removal, unmark it and increase the unconfigured proxies so
|
|
|
|
that we try to restart it if we need to. Afterwards, check if
|
|
|
|
a transport_t for 'transport' used to exist before the SIGHUP
|
|
|
|
and make sure it doesn't get deleted because we might reuse
|
|
|
|
it. */
|
|
|
|
if (mp->marked_for_removal) {
|
|
|
|
mp->marked_for_removal = 0;
|
|
|
|
unconfigured_proxies_n++;
|
|
|
|
}
|
|
|
|
|
2011-10-07 14:13:41 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(transport_list, const char *, transport) {
|
|
|
|
old_transport = transport_get_by_name(transport);
|
|
|
|
if (old_transport)
|
|
|
|
old_transport->marked_for_removal = 0;
|
|
|
|
} SMARTLIST_FOREACH_END(transport);
|
2011-09-11 20:29:12 +02:00
|
|
|
}
|
2011-08-12 21:33:05 +02:00
|
|
|
|
2011-10-07 14:13:41 +02:00
|
|
|
SMARTLIST_FOREACH(transport_list, const char *, transport,
|
|
|
|
add_transport_to_proxy(transport, mp));
|
2011-08-12 21:33:05 +02:00
|
|
|
free_execve_args(proxy_argv);
|
|
|
|
}
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Frees the array of pointers in <b>arg</b> used as arguments to
|
2011-09-23 17:50:56 +02:00
|
|
|
execve(2). */
|
2011-07-13 19:00:28 +02:00
|
|
|
static INLINE void
|
|
|
|
free_execve_args(char **arg)
|
|
|
|
{
|
|
|
|
char **tmp = arg;
|
|
|
|
while (*tmp) /* use the fact that the last element of the array is a
|
|
|
|
NULL pointer to know when to stop freeing */
|
|
|
|
_tor_free(*tmp++);
|
|
|
|
|
|
|
|
tor_free(arg);
|
|
|
|
}
|
|
|
|
|
2011-09-23 17:50:56 +02:00
|
|
|
/** Tor will read its config.
|
|
|
|
* Prepare the managed proxy list so that proxies not used in the new
|
|
|
|
* config will shutdown, and proxies that need to spawn different
|
|
|
|
* transports will do so. */
|
2011-09-11 20:29:12 +02:00
|
|
|
void
|
|
|
|
pt_prepare_proxy_list_for_config_read(void)
|
|
|
|
{
|
|
|
|
if (!managed_proxy_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
|
|
|
|
/* Destroy unconfigured proxies. */
|
|
|
|
if (mp->conf_state != PT_PROTO_COMPLETED) {
|
2011-10-24 15:59:11 +02:00
|
|
|
managed_proxy_destroy(mp, 1);
|
|
|
|
unconfigured_proxies_n--;
|
|
|
|
continue;
|
2011-09-11 20:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
|
|
|
|
|
|
|
|
mp->marked_for_removal = 1;
|
|
|
|
mp->got_hup = 1;
|
|
|
|
SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
|
|
|
|
smartlist_clear(mp->transports_to_launch);
|
|
|
|
} SMARTLIST_FOREACH_END(mp);
|
|
|
|
|
|
|
|
tor_assert(unconfigured_proxies_n == 0);
|
|
|
|
}
|
|
|
|
|
2011-09-23 17:50:56 +02:00
|
|
|
/** The tor config was read.
|
|
|
|
* Destroy all managed proxies that were marked by a previous call to
|
|
|
|
* prepare_proxy_list_for_config_read() and are not used by the new
|
|
|
|
* config. */
|
2011-09-11 20:29:12 +02:00
|
|
|
void
|
|
|
|
sweep_proxy_list(void)
|
|
|
|
{
|
|
|
|
if (!managed_proxy_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
|
|
|
|
if (mp->marked_for_removal) {
|
|
|
|
SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
|
2011-10-24 15:59:11 +02:00
|
|
|
managed_proxy_destroy(mp, 1);
|
2011-09-11 20:29:12 +02:00
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(mp);
|
|
|
|
}
|
|
|
|
|
2011-07-13 19:00:28 +02:00
|
|
|
/** Release all storage held by the pluggable transports subsystem. */
|
|
|
|
void
|
|
|
|
pt_free_all(void)
|
|
|
|
{
|
2011-09-11 20:29:12 +02:00
|
|
|
if (managed_proxy_list) {
|
2011-07-13 19:00:28 +02:00
|
|
|
/* If the proxy is in PT_PROTO_COMPLETED, it has registered its
|
|
|
|
transports and it's the duty of the circuitbuild.c subsystem to
|
|
|
|
free them. Otherwise, it hasn't registered its transports yet
|
|
|
|
and we should free them here. */
|
2011-09-11 20:29:12 +02:00
|
|
|
SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp,
|
2011-10-24 15:59:11 +02:00
|
|
|
managed_proxy_destroy(mp, 1));
|
2011-07-13 19:00:28 +02:00
|
|
|
|
2011-09-11 20:29:12 +02:00
|
|
|
smartlist_free(managed_proxy_list);
|
|
|
|
managed_proxy_list=NULL;
|
2011-07-13 19:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|