summaryrefslogtreecommitdiff
path: root/opentracker.c
blob: 698669c1be33088773c7dfe663cb40f12b7fa227 (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
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.
   Some of the stuff below is stolen from Fefes example libowfat httpd.

   $Id$ */

/* System */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#ifdef WANT_SYSLOGS
#include <syslog.h>
#endif

/* Libowfat */
#include "io.h"
#include "iob.h"

/* Opentracker */
#include "ot_accesslist.h"
#include "ot_config.h"
#include "ot_hex.h"
#include "ot_http.h"
#include "ot_ip.h"
#include "ot_livesync.h"
#include "ot_mutex.h"
#include "ot_random.h"
#include "ot_scan.h"
#include "ot_socket.h"
#include "ot_stats.h"
#include "ot_udp.h"
#include "trackerlogic.h"

/* Globals */
time_t              g_now_seconds;
uint32_t            g_tracker_id;
volatile int        g_opentracker_running = 1;
int                 g_self_pipe[2];


static void         panic(const char *routine) __attribute__((noreturn));
static void         panic(const char *routine) {
  fprintf(stderr, "%s: %s\n", routine, strerror(errno));
  exit(111);
}

static void signal_handler(int s) {
  if (s == SIGINT) {
    /* Any new interrupt signal quits the application */
    signal(SIGINT, SIG_DFL);

    /* Tell all other threads to not acquire any new lock on a bucket
       but cancel their operations and return */
    g_opentracker_running = 0;

    trackerlogic_deinit();

#ifdef WANT_SYSLOGS
    closelog();
#endif

    exit(0);
  }
}

static void defaul_signal_handlers(void) {
  sigset_t signal_mask;
  sigemptyset(&signal_mask);
  sigaddset(&signal_mask, SIGPIPE);
  sigaddset(&signal_mask, SIGHUP);
  sigaddset(&signal_mask, SIGINT);
  sigaddset(&signal_mask, SIGALRM);
  pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
}

static void install_signal_handlers(void) {
  struct sigaction sa;
  sigset_t         signal_mask;
  sigemptyset(&signal_mask);

  sa.sa_handler = signal_handler;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART;
  if ((sigaction(SIGINT, &sa, NULL) == -1) || (sigaction(SIGALRM, &sa, NULL) == -1))
    panic("install_signal_handlers");

  sigaddset(&signal_mask, SIGINT);
  pthread_sigmask(SIG_UNBLOCK, &signal_mask, NULL);
}


static ssize_t header_complete(char *request, ssize_t byte_count) {
  ssize_t i = 0, state = 0;

  for (i = 1; i < byte_count; i += 2)
    if (request[i] <= 13) {
      i--;
      for (state = 0; i < byte_count; ++i) {
        char c = request[i];
        if (c == '\r' || c == '\n')
          state = (state >> 2) | ((c << 6) & 0xc0);
        else
          break;
        if (state >= 0xa0 || state == 0x99)
          return i + 1;
      }
    }
  return 0;
}

static void handle_dead(const int64_t sock) {
  struct http_data *cookie = io_getcookie(sock);
  if (cookie) {
    size_t i;
    for (i = 0; i < cookie->batches; ++i)
      iob_reset(cookie->batch + i);
    free(cookie->batch);
    buf_reset(&cookie->request);
    if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER))
      mutex_workqueue_canceltask(sock);
    free(cookie);
  }
  io_close(sock);
}

static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
  struct http_data *cookie     = io_getcookie(sock);
  ssize_t           byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE);

  if (byte_count == 0 || byte_count == -3) {
    handle_dead(sock);
    return;
  }

  if (byte_count == -1)
    return;

  ws->request      = NULL;
  ws->request_size = 0;
  ws->header_size  = 0;
  ws->keep_alive   = 0;

  /* If we get the whole request in one packet, handle it without copying */
  if (!cookie->request.data) {
    if ((ws->header_size = header_complete(ws->inbuf, byte_count))) {
      ws->request      = ws->inbuf;
      ws->request_size = byte_count;
      http_handle_request(sock, ws);
    } else if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count))
      handle_dead(sock);
    return;
  }

  if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count) || cookie->request.size > G_INBUF_SIZE) {
    http_issue_error(sock, ws, CODE_HTTPERROR_500);
    return;
  }

  while ((ws->header_size = header_complete(cookie->request.data, (ssize_t)cookie->request.size))) {
    ws->request      = cookie->request.data;
    ws->request_size = (ssize_t)cookie->request.size;
    http_handle_request(sock, ws);
#ifdef WANT_KEEPALIVE
    if (!ws->keep_alive)
#endif
      return;
  }
}

static void handle_write(const int64_t sock) {
  struct http_data *cookie = io_getcookie(sock);
  size_t            i;
  int               chunked = 0;

  /* Look for the first io_batch still containing bytes to write */
  if (cookie) {
    if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)
      chunked = 1;

    for (i = 0; i < cookie->batches; ++i) {
      if (cookie->batch[i].bytesleft) {
        int64_t res = iob_send(sock, cookie->batch + i);

        if (res == -3) {
          handle_dead(sock);
          return;
        }

        if (!cookie->batch[i].bytesleft)
          continue;

        if (res == -1 || res > 0 || i < cookie->batches - 1)
          return;
      }
    }
  }

  /* In a chunked transfer after all batches accumulated have been sent, wait for the next one */
  if (chunked)
    io_dontwantwrite(sock);
  else
    handle_dead(sock);
}

static void handle_accept(const int64_t serversocket) {
  struct http_data *cookie;
  int64_t           sock;
  ot_ip6            ip;
  uint16_t          port;
  tai6464           t;

  while ((sock = socket_accept(serversocket, ip, &port)) != -1) {

    /* Put fd into a non-blocking mode */
    io_nonblock(sock);

    if (!io_fd(sock) || !(cookie = (struct http_data *)malloc(sizeof(struct http_data)))) {
      io_close(sock);
      continue;
    }
    memset(cookie, 0, sizeof(struct http_data));
    memcpy(cookie->ip, ip, sizeof(ot_ip6));

    io_setcookie(sock, cookie);
    io_wantread(sock);

    stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip);

    /* That breaks taia encapsulation. But there is no way to take system
       time this often in FreeBSD and libowfat does not allow to set unix time */
    taia_uint(&t, 0); /* Clear t */
    tai_unix(&(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT));
    io_timeout(sock, t);
  }
  io_eagain(serversocket);
}

static void *server_mainloop(void *args) {
  struct ot_workstruct ws;
  time_t               next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
  struct iovec        *iovector;
  size_t               iovec_entries;
  int                  is_partial;

  (void)args;

  /* Initialize our "thread local storage" */
  ws.inbuf  = malloc(G_INBUF_SIZE);
  ws.outbuf = malloc(G_OUTBUF_SIZE);
#ifdef _DEBUG_HTTPERROR
  ws.debugbuf = malloc(G_DEBUGBUF_SIZE);
#endif

  if (!ws.inbuf || !ws.outbuf)
    panic("Initializing worker failed");

  random_bytes(ws.rand48_state, sizeof(ws.rand48_state));

  for (;;) {
    int64_t sock;

    io_wait();

    while ((sock = io_canread()) != -1) {
      const void *cookie = io_getcookie(sock);
      if ((intptr_t)cookie == FLAG_TCP)
        handle_accept(sock);
      else if ((intptr_t)cookie == FLAG_UDP)
        handle_udp6(sock, &ws);
      else if ((intptr_t)cookie == FLAG_SELFPIPE)
        io_tryread(sock, ws.inbuf, G_INBUF_SIZE);
      else
        handle_read(sock, &ws);
    }

    while ((sock = mutex_workqueue_popresult(&iovec_entries, &iovector, &is_partial)) != -1)
      http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial);

    while ((sock = io_canwrite()) != -1)
      handle_write(sock);

    if (g_now_seconds > next_timeout_check) {
      while ((sock = io_timeouted()) != -1)
        handle_dead(sock);
      next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
    }

    livesync_ticker();
  }
  return 0;
}


static void listen_bind_one(ot_listen *entry) {
  socket_sharing sharing = entry->shared ? SOCKET_SHARED : SOCKET_EXCLUSIVE;

#ifdef _DEBUG
  {
    char *protos[] = {"TCP", "UDP", "UDP mcast"};
    char  _debug[512];
    int   off  = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[entry->proto]);
    off       += ip_format(_debug + off, entry->ip);
    snprintf(_debug + off, sizeof(_debug) - off, "]:%d...", entry->port);
    fputs(_debug, stderr);
  }
#endif

#ifdef WANT_SYNC_LIVE
  if (entry->proto == FLAG_MCA) {
    livesync_bind_mcast(entry->ip, entry->port);
#ifdef _DEBUG
    fputs(" success.\n", stderr);
#endif
    return;
  }
#endif

  if (entry->proto == FLAG_TCP) {
    if ((entry->sock = socket_tcp_listen(entry->ip, entry->port, sharing)) == -1)
      panic("socket_tcp_listen");
  } else {
    if ((entry->sock = socket_udp_bind(entry->ip, entry->port, sharing)) == -1)
      panic("socket_udp_bind");
  }

  if (!io_fd(entry->sock))
    panic("io_fd");

  io_setcookie(entry->sock, (void *)entry->proto);
  io_wantread(entry->sock);

#ifdef _DEBUG
  fputs(" success.\n", stderr);
#endif
}

/* Bound in the order the entries were given.
   Note: On macOS SO_REUSEPORT delivers everything to the socket that bound last. */
static void listen_bind_all(void) {
  size_t     count, i;
  ot_listen *entries = config_listen_entries(&count);

  for (i = 0; i < count; ++i)
    listen_bind_one(entries + i);
}

static void listen_start_udp_workers(void) {
  size_t     count, i;
  ot_listen *entries = config_listen_entries(&count);

  udp_init();

  for (i = 0; i < count; ++i) {
    ot_listen *entry = entries + i;

    if (entry->proto != FLAG_UDP || entry->workers <= 0)
      continue;

    io_block(entry->sock);
    udp_workers_install(entry->sock, (unsigned int)entry->workers);
  }
}



void load_state(const char *const state_filename) {
  FILE              *state_filehandle;
  char               inbuf[512];
  ot_hash  infohash;
  uint64_t base, downcount;
  int      consumed;

  state_filehandle = fopen(state_filename, "r");

  if (state_filehandle == NULL) {
    fprintf(stderr, "Warning: Can't open config file: %s.", state_filename);
    return;
  }

  /* We do ignore anything that is not of the form "^[:xdigit:]:\d+:\d+" */
  while (fgets(inbuf, sizeof(inbuf), state_filehandle)) {
    size_t i;

    if (!hex_tohash(infohash, inbuf))
      continue;
    i = 2 * sizeof(ot_hash);

    if (inbuf[i++] != ':' || !(consumed = scan_udec(inbuf + i, &base, UINT64_MAX)))
      continue;
    i += consumed;
    if (inbuf[i++] != ':' || !(consumed = scan_udec(inbuf + i, &downcount, UINT64_MAX)))
      continue;
    add_torrent_from_saved_state(infohash, base, downcount);
  }

  fclose(state_filehandle);
}

int drop_privileges(const char *const serveruser, const char *const serverdir) {
  struct passwd *pws = NULL;

#ifdef _DEBUG
  if (!geteuid())
    fprintf(stderr, "Dropping to user %s.\n", serveruser);
  if (serverdir)
    fprintf(stderr, "ch%s'ing to directory %s.\n", geteuid() ? "dir" : "root", serverdir);
#endif

  /* Grab pws entry before chrooting */
  pws = getpwnam(serveruser);
  endpwent();

  if (geteuid() == 0) {
    /* Running as root: chroot and drop privileges */
    if (serverdir && chroot(serverdir)) {
      fprintf(stderr, "Could not chroot to %s, because: %s\n", serverdir, strerror(errno));
      return -1;
    }

    if (chdir("/"))
      panic("chdir() failed after chrooting: ");

    /* If we can't find server user, revert to nobody's default uid */
    if (!pws) {
      fprintf(stderr, "Warning: Could not get password entry for %s. Reverting to uid -2.\n", serveruser);
      if (setegid((gid_t)-2) || setgid((gid_t)-2) || setuid((uid_t)-2) || seteuid((uid_t)-2))
        panic("Could not set uid to value -2");
    } else {
      if (setegid(pws->pw_gid) || setgid(pws->pw_gid) || setuid(pws->pw_uid) || seteuid(pws->pw_uid))
        panic("Could not set uid to specified value");
    }

    if (geteuid() == 0 || getegid() == 0)
      panic("Still running with root privileges?!");
  } else {
    /* Normal user, just chdir() */
    if (serverdir && chdir(serverdir)) {
      fprintf(stderr, "Could not chroot to %s, because: %s\n", serverdir, strerror(errno));
      return -1;
    }
  }

  return 0;
}

/* Maintain our copy of the clock. time() on BSDs is very expensive. */
static void *time_caching_worker(void *args) {
  (void)args;
  while (1) {
    g_now_seconds = time(NULL);
    sleep(5);
  }
  return NULL;
}

int main(int argc, char **argv) {
  pthread_t thread_id; /* time cacher */

  random_init();
  config_parse(argc, argv);


  listen_bind_all();

  defaul_signal_handlers();

#ifdef WANT_SYSLOGS
  openlog("opentracker", 0, LOG_USER);
  setlogmask(LOG_UPTO(LOG_INFO));
#endif

  if (drop_privileges(g_serveruser ? g_serveruser : "nobody", g_serverdir) == -1)
    panic("drop_privileges failed, exiting. Last error");

  g_now_seconds = time(NULL);
  pthread_create(&thread_id, NULL, time_caching_worker, NULL);

  /* Create our self pipe which allows us to interrupt mainloops
     io_wait in case some data is available to send out */
  if (pipe(g_self_pipe) == -1)
    panic("selfpipe failed: ");
  if (!io_fd(g_self_pipe[0]))
    panic("selfpipe io_fd failed: ");
  if (!io_fd(g_self_pipe[1]))
    panic("selfpipe io_fd failed: ");
  io_setcookie(g_self_pipe[0], (void *)FLAG_SELFPIPE);
  io_wantread(g_self_pipe[0]);

  /* Init all sub systems. This call may fail with an exit() */
  trackerlogic_init();

#ifdef _DEBUG_RANDOMTORRENTS
  fprintf(stderr, "DEBUG: Generating %d random peers on random torrents. This may take a while. (Setting RANDOMTORRENTS in trackerlogic.h)\n", RANDOMTORRENTS);
  trackerlogic_add_random_torrents(RANDOMTORRENTS);
  fprintf(stderr, "... done.\n");
#endif

  if (g_statefile)
    load_state(g_statefile);

  install_signal_handlers();

  listen_start_udp_workers();

  server_mainloop(0);

  return 0;
}