summaryrefslogtreecommitdiff
path: root/src/or/transports.c
blob: 49b0e131af6fc51c614efb3a09d6c169503ad0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* Copyright (c) 2011, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file pluggable_transports.c
 * \brief Pluggable Transports related code.
 **/

#define PT_PRIVATE
#include "or.h"
#include "config.h"
#include "circuitbuild.h"
#include "pluggable_transports.h"

/* ASN TIDY THESE UP*/
static void set_environ(char ***envp, const char *method,
                        int is_server);
static INLINE int proxy_configuration_finished(managed_proxy_t *mp);

static void managed_proxy_destroy(managed_proxy_t *mp,
                                  int also_free_transports);
static void register_proxy_transports(managed_proxy_t *mp);
static void handle_finished_proxy(managed_proxy_t *mp);
static void configure_proxy(managed_proxy_t *mp);

static void parse_method_error(char *line, int is_server_method);
#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. */
static smartlist_t *unconfigured_proxy_list = NULL;
/** Number of unconfigured managed proxies. */
static int n_unconfigured_proxies = 0;

/*  The main idea is:

    A managed proxy is represented by a managed_proxy_t struct and can
    spawn multiple transports.

    unconfigured_proxy_list is a list of all the unconfigured managed
    proxies; everytime we spawn a managed proxy we add it in that
    list.
    In every run_scheduled_event() tick, we attempt to configure each
    managed proxy further, using the configuration protocol defined in
    the 180_pluggable_transport.txt proposal.

    When a managed proxy is fully configured, we register all its
    transports to the circuitbuild.c subsystem - like we do with
    external proxies - and then free the managed proxy struct
    since it's no longer needed. */

/** Return true if there are still unconfigured managed proxies. */
int
pt_proxies_configuration_pending(void)
{
  return !!n_unconfigured_proxies;
}

/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
 *  arguments. If <b>is_server</b>, launch a server proxy. */
int
pt_managed_launch_proxy(const char *method,
                        char **proxy_argv, int is_server)
{
  char **envp=NULL;
  int retval;
  FILE *stdout_read = NULL;
  int stdout_pipe=-1, stderr_pipe=-1;

  /* prepare the environment variables for the managed proxy */
  set_environ(&envp, method, is_server);

  /* ASN we should probably check if proxy_argv[0] is executable by our user */
  retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
                                &stderr_pipe, (const char **)proxy_argv,
                                (const char **)envp);
  if (retval < 0) {
    log_warn(LD_GENERAL, "Spawn failed");
    return -1;
  }

  /* free the memory allocated for the execve() */
  free_execve_args(envp);
  free_execve_args(proxy_argv);

  /* Set stdout/stderr pipes to be non-blocking */
  fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
  /* Open the buffered IO streams */
  stdout_read = fdopen(stdout_pipe, "r");

  log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);

  /* create a managed proxy */
  managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
  mp->conf_state = PT_PROTO_INFANT;
  mp->stdout = stdout_read;
  mp->transports = smartlist_create();

  /* register the managed proxy */
  if (!unconfigured_proxy_list)
    unconfigured_proxy_list = smartlist_create();
  smartlist_add(unconfigured_proxy_list, mp);

  n_unconfigured_proxies++; /* ASN should we care about overflows here?
                               I say no. */

  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)
{
  log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
  SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list,  managed_proxy_t *, mp) {
    if (proxy_configuration_finished(mp)) /* finished managed proxies
                                             shouldn't be here */
      assert(0);

    configure_proxy(mp);

  } SMARTLIST_FOREACH_END(mp);
}

/** Receive input from the managed proxy <b>mp</b> to get closer to
 *  finally configuring it. */
static void
configure_proxy(managed_proxy_t *mp)
{
  enum stream_status r;
  char stdout_buf[200];

  while (1) {
    memset(stdout_buf, 0, sizeof(stdout_buf));

    r = get_string_from_pipe(mp->stdout, stdout_buf,
                             sizeof(stdout_buf) - 1);

    if (r == ST_CLOSED || r == ST_TERM) {
      log_warn(LD_GENERAL, "Managed proxy stream closed. "
               "Most probably application stopped running");
      mp->conf_state = PT_PROTO_BROKEN;
    } else if (r == ST_EAGAIN) {
      return;
    } else {
      tor_assert(r == ST_OKAY);
      handle_proxy_line(stdout_buf, mp);
    }

    /* if the proxy finished configuring, exit the loop. */
    if (proxy_configuration_finished(mp)) {
      handle_finished_proxy(mp);
      return;
    }
  }
}

/** 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: */
    managed_proxy_destroy(mp, 1); /* destroy it and all its transports */
    break;
  case PT_PROTO_CONFIGURED: /* if configured correctly: */
    register_proxy_transports(mp); /* register all its transports, */
    mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
    managed_proxy_destroy(mp, 0); /* destroy the managed proxy struct,
                                     keeping the transports intact */
    break;
  default:
    log_warn(LD_CONFIG, "Unfinished managed proxy in "
             "handle_finished_proxy().");
    assert(0);
  }

  n_unconfigured_proxies--;
  tor_assert(n_unconfigured_proxies >= 0);
}

/** Register all the transports supported by managed proxy <b>mp</b>. */
static void
register_proxy_transports(managed_proxy_t *mp)
{
  SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
    if (transport_add(t)<0) {
      log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
      transport_free(t);
    } else {
      log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
    }
  } SMARTLIST_FOREACH_END(t);
}

/** Free memory allocated by managed proxy <b>mp</b>.
 * If <b>also_free_transports</b> is set, also free the transports
 * associated with this managed proxy. */
static void
managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
{
  /* transport_free() all its transports */
  if (also_free_transports)
    SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));

  /* free the transports smartlist */
  smartlist_clear(mp->transports);
  smartlist_free(mp->transports);

  /* remove it from the list of managed proxies */
  smartlist_remove(unconfigured_proxy_list, mp);

  /* close its stdout stream */
  fclose(mp->stdout);

  tor_free(mp);
}

/** Return true if the configuration of the managed proxy <b>mp</b> is
    finished. */
static INLINE int
proxy_configuration_finished(managed_proxy_t *mp)
{
  return (mp->conf_state == PT_PROTO_CONFIGURED ||
          mp->conf_state == PT_PROTO_BROKEN);
}

/** Handle a configuration protocol <b>line</b> received from a
 *  managed proxy <b>mp</b>. */
void
handle_proxy_line(char *line, managed_proxy_t *mp)
{
  printf("Judging line: %s\n", line);

  if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
    log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
             "Discarding");
    goto err;
  }

  if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
    if (mp->conf_state != PT_PROTO_INFANT)
      goto err;

    parse_env_error(line);
    goto err;
  } else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
    if (mp->conf_state != PT_PROTO_INFANT)
      goto err;

    log_warn(LD_CONFIG, "Managed proxy could not pick a "
             "configuration protocol version.");
    goto err;
  } else if (!strncmp(line, PROTO_NEG_SUCCESS,
                      strlen(PROTO_NEG_SUCCESS))) {
    if (mp->conf_state != PT_PROTO_INFANT)
      goto err;

    if (parse_version(line,mp) < 0)
      goto err;

    tor_assert(mp->conf_protocol != 0);
    mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
    return;
  } else if (!strncmp(line, PROTO_CMETHODS_DONE,
                      strlen(PROTO_CMETHODS_DONE))) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    log_warn(LD_CONFIG, "Client managed proxy configuration completed!");
    mp->conf_state = PT_PROTO_CONFIGURED;
    return;
  } else if (!strncmp(line, PROTO_SMETHODS_DONE,
                      strlen(PROTO_SMETHODS_DONE))) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    log_warn(LD_CONFIG, "Server managed proxy configuration completed!");
    mp->conf_state = PT_PROTO_CONFIGURED;
    return;
  } else if (!strncmp(line, PROTO_CMETHOD_ERROR,
                      strlen(PROTO_CMETHOD_ERROR))) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    parse_client_method_error(line);
    goto err;
  } else if (!strncmp(line, PROTO_SMETHOD_ERROR,
                      strlen(PROTO_SMETHOD_ERROR))) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    parse_server_method_error(line);
    goto err;
  } else if (!strncmp(line, PROTO_CMETHOD, strlen(PROTO_CMETHOD))) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    if (parse_cmethod_line(line, mp) < 0)
      goto err;

    return;
  } else if (!strncmp(line, PROTO_SMETHOD, strlen(PROTO_SMETHOD))) {
    if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
      goto err;

    if (parse_smethod_line(line, mp) < 0)
      goto err;

    return;
  }

  log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);

 err:
  mp->conf_state = PT_PROTO_BROKEN;
  return;
}

/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
void
parse_env_error(char *line)
{
  tor_assert(!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR)));

  /* (Length of the protocol string) plus (a space) and (the first char of
     the error message) */
  if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
    log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
             "message.", PROTO_ENV_ERROR);

  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
parse_version(char *line, managed_proxy_t *mp)
{
 tor_assert(!strncmp(line, PROTO_NEG_SUCCESS, strlen(PROTO_NEG_SUCCESS)));

  if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
    log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
             PROTO_NEG_SUCCESS);
    return -1;
  }

  if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) {
    log_warn(LD_CONFIG, "We don't support version '%s'. "
             "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
parse_method_error(char *line, int is_server)
{
  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);
}

/** Parses an SMETHOD <b>line</b>. */
int
parse_smethod_line(char *line, managed_proxy_t *mp)
{
  int r;
  smartlist_t *items = NULL;

  char *method_name=NULL;

  char *addrport=NULL;
  tor_addr_t addr;
  uint16_t port = 0;

  items = smartlist_create();
  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);

  addrport = smartlist_get(items, 2);
  if (tor_addr_port_parse(addrport, &addr, &port)<0) {
    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;
  }

  /* For now, notify the user so that he knows where the server
     transport is listening. */
  log_warn(LD_CONFIG, "Server transport %s at %s:%d.",
           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
parse_cmethod_line(char *line, managed_proxy_t *mp)
{
  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;

  items = smartlist_create();
  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);

  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);
  if (tor_addr_port_parse(addrport, &addr, &port)<0) {
    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;
  }

  transport = transport_create(&addr, port, method_name, socks_ver);
  if (!transport)
    goto err;

  smartlist_add(mp->transports, transport);

  log_warn(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
           "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;
}

/** Prepares the <b>envp</b> of a pluggable transport managed proxy
 *
 *  <b>method</b> is a line with transport methods to be launched.
 *  If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
static void
set_environ(char ***envp, const char *method, int is_server)
{
  or_options_t *options = get_options();
  char **tmp=NULL;
  char *state_loc=NULL;

  int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;

  /* allocate enough space for our env. vars and a NULL pointer */
  *envp = tor_malloc(sizeof(char*)*(n_envs+1));
  tmp = *envp;

  /* these should all be customizable */
  tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
  tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
  state_loc = get_datadir_fname("pt_state/");
  tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
  tor_free(state_loc);
  tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
  if (is_server) {
    /* ASN check for ORPort values, should we be here if it's 0? */
    tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
    tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=127.0.0.1:0");
    tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
    tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
  } else {
    tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
  }
  *tmp = NULL;
}

/* ASN is this too ugly/stupid? */
/** Frees the array of pointers in <b>arg</b> used as arguments to
    execve. */
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);
}

/** Release all storage held by the pluggable transports subsystem. */
void
pt_free_all(void)
{
  if (unconfigured_proxy_list) {
    /* 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. */
    SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
      if (mp->conf_state == PT_PROTO_COMPLETED)
        managed_proxy_destroy(mp,0);
      else
        managed_proxy_destroy(mp,1);
    } SMARTLIST_FOREACH_END(mp);

    smartlist_clear(unconfigured_proxy_list);
    smartlist_free(unconfigured_proxy_list);
    unconfigured_proxy_list=NULL;
  }
}