summaryrefslogtreecommitdiff
path: root/src/ot_config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ot_config.c')
-rw-r--r--src/ot_config.c612
1 files changed, 612 insertions, 0 deletions
diff --git a/src/ot_config.c b/src/ot_config.c
new file mode 100644
index 0000000..e689314
--- /dev/null
+++ b/src/ot_config.c
@@ -0,0 +1,612 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <ctype.h>
6#include <errno.h>
7#include <limits.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <unistd.h>
12
13/* Opentracker */
14#include "ot_accesslist.h"
15#include "ot_event.h"
16#include "ot_config.h"
17#include "ot_http.h"
18#include "ot_ip.h"
19#include "ot_livesync.h"
20#include "ot_scan.h"
21#include "ot_stats.h"
22#include "trackerlogic.h"
23
24const char *g_redirecturl;
25const char *g_serverdir;
26const char *g_serveruser;
27const char *g_statefile;
28
29#define OT_LISTEN_INHERIT (-1)
30
31static ot_listen *g_listen;
32static size_t g_listen_count;
33static int g_listen_shared;
34static int g_listen_workers_udp = 1;
35static int g_listen_workers_tcp = 0;
36
37static void panic(const char *routine) __attribute__((noreturn));
38static void panic(const char *routine) {
39 fprintf(stderr, "%s: %s\n", routine, strerror(errno));
40 exit(111);
41}
42
43static void listen_append(const ot_ip6 ip, uint16_t port, PROTO_FLAG proto, int shared, int workers) {
44 ot_listen *grown = realloc(g_listen, (g_listen_count + 1) * sizeof(ot_listen));
45 ot_listen *entry;
46
47 if (!grown)
48 panic("listen_append");
49 g_listen = grown;
50
51 entry = g_listen + g_listen_count++;
52 memcpy(entry->ip, ip, sizeof(ot_ip6));
53 entry->port = port;
54 entry->proto = proto;
55 entry->shared = shared;
56 entry->workers = workers;
57 entry->sock = -1;
58}
59
60typedef enum { LISTEN_OPTION_UNKNOWN, LISTEN_OPTION_SHARED, LISTEN_OPTION_WORKERS } listen_option_kind;
61
62typedef struct {
63 const char *name;
64 size_t len;
65 listen_option_kind kind;
66 int takes_value;
67} listen_option_key;
68
69#define LISTEN_OPTION_KEY(s, k, v) {s, sizeof(s) - 1, k, v}
70
71static const listen_option_key listen_option_keys[] = {
72 LISTEN_OPTION_KEY("shared", LISTEN_OPTION_SHARED, 0),
73 LISTEN_OPTION_KEY("workers", LISTEN_OPTION_WORKERS, 1),
74};
75static const size_t listen_option_count = sizeof(listen_option_keys) / sizeof(listen_option_keys[0]);
76
77static listen_option_kind parse_listen_option(char **p) {
78 size_t i;
79
80 for (i = 0; i < listen_option_count; ++i) {
81 const listen_option_key *key = &listen_option_keys[i];
82 char delimiter;
83
84 if (strncmp(*p, key->name, key->len))
85 continue;
86 /* The match guarantees *p is at least key->len bytes, so this is in bounds */
87 delimiter = (*p)[key->len];
88 if (key->takes_value ? delimiter != '=' : (delimiter && !isspace((unsigned char)delimiter)))
89 continue;
90
91 *p += key->len + (key->takes_value ? 1 : 0);
92 return key->kind;
93 }
94 return LISTEN_OPTION_UNKNOWN;
95}
96
97/* Options trailing an address in a listen statement, in any order */
98static int listen_scan_options(char *p, int *shared, int *workers) {
99 uint64_t value;
100
101 for (;;) {
102 while (isspace((unsigned char)*p))
103 ++p;
104 if (!*p)
105 return 1;
106
107 switch (parse_listen_option(&p)) {
108 case LISTEN_OPTION_SHARED:
109 *shared = 1;
110 break;
111 case LISTEN_OPTION_WORKERS: {
112 size_t off = scan_uint(p, &value, UINT_MAX);
113
114 if (!off)
115 return 0;
116 *workers = (int)value;
117 p += off;
118 break;
119 }
120 default:
121 return 0;
122 }
123 }
124}
125
126
127static const char *set_config_option(const char **option, char *value) {
128#ifdef _DEBUG
129 fprintf(stderr, "Setting config option: %s\n", value);
130#endif
131 while (isspace((unsigned char)*value))
132 ++value;
133 free((char *)*option);
134 return *option = strdup(value);
135}
136
137static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) {
138 const char *s = src;
139 uint64_t value;
140 int off, bracket = 0;
141 while (isspace((unsigned char)*s))
142 ++s;
143 if (*s == '[')
144 ++s, ++bracket; /* for v6 style notation */
145 if (!(off = ip_parse(s, ip)))
146 return 0;
147 s += off;
148 if (bracket && *s == ']')
149 ++s;
150 if (*s == 0 || isspace((unsigned char)*s))
151 return s - src;
152 if (!ip_is_v4mapped(ip)) {
153 if (*s != ':' && *s != '.')
154 return 0;
155 if (!bracket && *(s) == ':')
156 return 0;
157 s++;
158 } else {
159 if (*(s++) != ':')
160 return 0;
161 }
162 if (!(off = scan_uint(s, &value, 65535)))
163 return 0;
164 *port = (uint16_t)value;
165 return off + s - src;
166}
167
168static int scan_ip6_net(const char *src, ot_net *net) {
169 const char *s = src;
170 uint64_t value;
171 int off;
172 while (isspace((unsigned char)*s))
173 ++s;
174 if (!(off = ip_parse(s, net->address)))
175 return 0;
176 s += off;
177 if (*s != '/')
178 net->bits = 128;
179 else {
180 s++;
181 if (!(off = scan_uint(s, &value, 128)))
182 return 0;
183 net->bits = (int)value;
184 if (ip_is_v4mapped(net->address))
185 net->bits += 96;
186 if (net->bits > 128)
187 return 0;
188 s += off;
189 }
190 return off + s - src;
191}
192
193typedef enum {
194 CONFIG_UNKNOWN,
195 CONFIG_TRACKER_ROOTDIR,
196 CONFIG_TRACKER_USER,
197 CONFIG_TRACKER_EMPTY_TIMEOUT,
198 CONFIG_LISTEN_TCP_UDP,
199 CONFIG_LISTEN_TCP,
200 CONFIG_LISTEN_UDP,
201 CONFIG_LISTEN_UDP_WORKERS,
202 CONFIG_LISTEN_TCP_WORKERS,
203 CONFIG_LISTEN_SHARED,
204 CONFIG_ACCESS_WHITELIST,
205 CONFIG_ACCESS_BLACKLIST,
206 CONFIG_ACCESS_FIFO_ADD,
207 CONFIG_ACCESS_FIFO_DELETE,
208 CONFIG_ACCESS_STATS,
209 CONFIG_ACCESS_STATS_PATH,
210 CONFIG_ACCESS_PROXY,
211 CONFIG_ACCESS_FULLSCRAPE,
212 CONFIG_TRACKER_REDIRECT_URL,
213 CONFIG_LIVESYNC_CLUSTER_NODE_IP,
214 CONFIG_LIVESYNC_CLUSTER_LISTEN,
215 CONFIG_LIVESYNC_CLUSTER_GROUP,
216} config_kind;
217
218typedef struct {
219 const char *name;
220 size_t len;
221 config_kind kind;
222} config_key;
223
224#define CONFIG_KEY(s, k) { s, sizeof(s) - 1, k }
225
226static const config_key config_keys[] = {
227 CONFIG_KEY("tracker.rootdir", CONFIG_TRACKER_ROOTDIR),
228 CONFIG_KEY("tracker.user", CONFIG_TRACKER_USER),
229 CONFIG_KEY("tracker.empty_torrent_timeout", CONFIG_TRACKER_EMPTY_TIMEOUT),
230 CONFIG_KEY("listen.tcp_udp", CONFIG_LISTEN_TCP_UDP),
231 CONFIG_KEY("listen.tcp", CONFIG_LISTEN_TCP),
232 CONFIG_KEY("listen.udp", CONFIG_LISTEN_UDP),
233 CONFIG_KEY("listen.udp.workers", CONFIG_LISTEN_UDP_WORKERS),
234 CONFIG_KEY("listen.tcp.workers", CONFIG_LISTEN_TCP_WORKERS),
235 CONFIG_KEY("listen.shared", CONFIG_LISTEN_SHARED),
236#ifdef WANT_ACCESSLIST_WHITE
237 CONFIG_KEY("access.whitelist", CONFIG_ACCESS_WHITELIST),
238#elif defined(WANT_ACCESSLIST_BLACK)
239 CONFIG_KEY("access.blacklist", CONFIG_ACCESS_BLACKLIST),
240#endif
241#ifdef WANT_DYNAMIC_ACCESSLIST
242 CONFIG_KEY("access.fifo_add", CONFIG_ACCESS_FIFO_ADD),
243 CONFIG_KEY("access.fifo_delete", CONFIG_ACCESS_FIFO_DELETE),
244#endif
245#ifdef WANT_RESTRICT_STATS
246 CONFIG_KEY("access.stats", CONFIG_ACCESS_STATS),
247#endif
248 CONFIG_KEY("access.stats_path", CONFIG_ACCESS_STATS_PATH),
249#ifdef WANT_IP_FROM_PROXY
250 CONFIG_KEY("access.proxy", CONFIG_ACCESS_PROXY),
251#endif
252#ifdef WANT_RESTRICT_FULLSCRAPE
253 CONFIG_KEY("access.fullscrape", CONFIG_ACCESS_FULLSCRAPE),
254#endif
255 CONFIG_KEY("tracker.redirect_url", CONFIG_TRACKER_REDIRECT_URL),
256#ifdef WANT_SYNC_LIVE
257 CONFIG_KEY("livesync.cluster.node_ip", CONFIG_LIVESYNC_CLUSTER_NODE_IP),
258 CONFIG_KEY("livesync.cluster.listen", CONFIG_LIVESYNC_CLUSTER_LISTEN),
259 CONFIG_KEY("livesync.cluster.group", CONFIG_LIVESYNC_CLUSTER_GROUP),
260#endif
261};
262static const size_t config_entry_count = sizeof(config_keys) / sizeof(config_keys[0]);
263
264static config_kind parse_config_key(char **p) {
265 size_t i;
266 for (i = 0; i < config_entry_count; ++i) {
267 const config_key *key = &config_keys[i];
268 if (strncmp(*p, key->name, key->len) == 0 && isspace((unsigned char)(*p)[key->len])) {
269 *p += key->len + 1;
270 /* Skip white spaces */
271 while (isspace((unsigned char)**p))
272 ++(*p);
273 return key->kind;
274 }
275 }
276 return CONFIG_UNKNOWN;
277}
278
279static int parse_configfile(char *config_filename) {
280 FILE *config_file_handle;
281 char inbuf[512];
282 ot_ip6 tmpip;
283#if defined(WANT_RESTRICT_STATS) || defined(WANT_RESTRICT_FULLSCRAPE) || defined(WANT_IP_FROM_PROXY) || defined(WANT_SYNC_LIVE)
284 ot_net tmpnet;
285#endif
286 int bound = 0;
287
288 config_file_handle = fopen(config_filename, "r");
289
290 if (config_file_handle == NULL) {
291 fprintf(stderr, "Warning: Can't open config file: %s.", config_filename);
292 return 0;
293 }
294
295 while (fgets(inbuf, sizeof(inbuf), config_file_handle)) {
296 char *p = inbuf, *end;
297 uint64_t value;
298 uint16_t tmpport = 6969;
299 config_kind kind;
300
301 /* Skip white spaces */
302 while (isspace((unsigned char)*p))
303 ++p;
304
305 /* Ignore comments and empty lines */
306 if ((*p == '#') || (*p == '\n') || (*p == 0))
307 continue;
308
309 /* consume trailing new lines and spaces */
310 end = p + strlen(p);
311 while (end > p && isspace((unsigned char)end[-1]))
312 *--end = 0;
313
314 /* Scan for commands */
315 kind = parse_config_key(&p);
316 switch (kind) {
317 case CONFIG_TRACKER_ROOTDIR:
318 set_config_option(&g_serverdir, p);
319 break;
320 case CONFIG_TRACKER_USER:
321 set_config_option(&g_serveruser, p);
322 break;
323 case CONFIG_TRACKER_EMPTY_TIMEOUT:
324 if (!scan_uint(p, &value, UINT_MAX))
325 goto parse_error;
326 g_empty_torrent_timeout = (ot_time)value;
327 break;
328 case CONFIG_LISTEN_TCP_UDP:
329 case CONFIG_LISTEN_TCP:
330 case CONFIG_LISTEN_UDP: {
331 int shared = OT_LISTEN_INHERIT;
332 int workers = OT_LISTEN_INHERIT;
333 int off = scan_ip6_port(p, tmpip, &tmpport);
334
335 if (!off || !listen_scan_options(p + off, &shared, &workers))
336 exerr("listen.tcp, listen.udp and listen.tcp_udp want an address, optionally a port and options.");
337 if (kind != CONFIG_LISTEN_UDP)
338 listen_append(tmpip, tmpport, FLAG_TCP, shared, workers), ++bound;
339 if (kind != CONFIG_LISTEN_TCP)
340 listen_append(tmpip, tmpport, FLAG_UDP, shared, workers), ++bound;
341 break;
342 }
343 case CONFIG_LISTEN_UDP_WORKERS:
344 if (!scan_uint(p, &value, UINT_MAX))
345 goto parse_error;
346 g_listen_workers_udp = (int)value;
347 break;
348 case CONFIG_LISTEN_TCP_WORKERS:
349 if (!scan_uint(p, &value, UINT_MAX))
350 goto parse_error;
351 g_listen_workers_tcp = (int)value;
352 break;
353 case CONFIG_LISTEN_SHARED:
354 if (!scan_uint(p, &value, 1))
355 goto parse_error;
356 g_listen_shared = (int)value;
357 break;
358#ifdef WANT_ACCESSLIST_WHITE
359 case CONFIG_ACCESS_WHITELIST:
360 set_config_option(&g_accesslist_filename, p);
361 break;
362#elif defined(WANT_ACCESSLIST_BLACK)
363 case CONFIG_ACCESS_BLACKLIST:
364 set_config_option(&g_accesslist_filename, p);
365 break;
366#endif
367#ifdef WANT_DYNAMIC_ACCESSLIST
368 case CONFIG_ACCESS_FIFO_ADD:
369 set_config_option(&g_accesslist_pipe_add, p);
370 break;
371 case CONFIG_ACCESS_FIFO_DELETE:
372 set_config_option(&g_accesslist_pipe_delete, p);
373 break;
374#endif
375#ifdef WANT_RESTRICT_STATS
376 case CONFIG_ACCESS_STATS:
377 if (!scan_ip6_net(p, &tmpnet))
378 goto parse_error;
379 accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_STAT);
380 break;
381#endif
382 case CONFIG_ACCESS_STATS_PATH:
383 set_config_option(&g_stats_path, p);
384 break;
385#ifdef WANT_IP_FROM_PROXY
386 case CONFIG_ACCESS_PROXY:
387 if (!scan_ip6_net(p, &tmpnet))
388 goto parse_error;
389 accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_PROXY);
390 break;
391#endif
392#ifdef WANT_RESTRICT_FULLSCRAPE
393 case CONFIG_ACCESS_FULLSCRAPE:
394 if (!scan_ip6_net(p, &tmpnet))
395 goto parse_error;
396 accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_FULLSCRAPE);
397 break;
398#endif
399 case CONFIG_TRACKER_REDIRECT_URL:
400 set_config_option(&g_redirecturl, p);
401 break;
402#ifdef WANT_SYNC_LIVE
403 case CONFIG_LIVESYNC_CLUSTER_GROUP:
404 if (!ip_parse(p, tmpip) || livesync_set_group(tmpip) == -1)
405 exerr("livesync.cluster.group wants a multicast address, out of 224.0.0.0/4.");
406 break;
407 case CONFIG_LIVESYNC_CLUSTER_NODE_IP:
408 if (!scan_ip6_net(p, &tmpnet))
409 goto parse_error;
410 accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_LIVESYNC);
411 break;
412 case CONFIG_LIVESYNC_CLUSTER_LISTEN:
413 tmpport = LIVESYNC_PORT;
414 {
415 int shared = OT_LISTEN_INHERIT, dummy = OT_LISTEN_INHERIT;
416 int off = scan_ip6_port(p, tmpip, &tmpport);
417
418 if (!off || !listen_scan_options(p + off, &shared, &dummy))
419 goto parse_error;
420 listen_append(tmpip, tmpport, FLAG_MCA, shared, OT_LISTEN_INHERIT);
421 }
422 break;
423#endif
424 default:
425 fprintf(stderr, "Unhandled line in config file: %s\n", inbuf);
426 break;
427 parse_error:
428 fprintf(stderr, "Parse error in config file: %s\n", inbuf);
429 }
430 }
431 fclose(config_file_handle);
432 return bound;
433}
434
435static void usage(char *name) {
436 fprintf(stderr,
437 "Usage: %s [-i ip] [-p port] [-P port] [-r redirect] [-d dir] [-u user] [-A ip[/bits]] [-f config] [-s livesyncport]"
438#ifdef WANT_ACCESSLIST_BLACK
439 " [-b blacklistfile]"
440#elif defined(WANT_ACCESSLIST_WHITE)
441 " [-w whitelistfile]"
442#endif
443 "\n",
444 name);
445}
446
447#define HELPLINE(opt, desc) fprintf(stderr, "\t%-10s%s\n", opt, desc)
448static void help(char *name) {
449 usage(name);
450
451 HELPLINE("-f config", "include and execute the config file");
452 HELPLINE("-i ip", "specify ip to bind to with next -[pP] (default: any, overrides preceeding ones)");
453 HELPLINE("-p port", "do bind to tcp port (default: 6969, you may specify more than one)");
454 HELPLINE("-P port", "do bind to udp port (default: 6969, you may specify more than one)");
455 HELPLINE("-r redirecturl", "specify url where / should be redirected to (default none)");
456 HELPLINE("-d dir", "specify directory to try to chroot to (default: \".\")");
457 HELPLINE("-u user", "specify user under whose privileges opentracker should run (default: \"nobody\")");
458 HELPLINE("-A ip[/bits]", "bless an ip address or net as admin address (e.g. to allow syncs from this address)");
459 HELPLINE("-v", "report version, event mechanism and the feature flags opentracker was built with");
460#ifdef WANT_ACCESSLIST_BLACK
461 HELPLINE("-b file", "specify blacklist file.");
462#elif defined(WANT_ACCESSLIST_WHITE)
463 HELPLINE("-w file", "specify whitelist file.");
464#endif
465
466 fprintf(stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -f ./opentracker.conf -i 10.1.1.23 -p 2710 -p 80\n");
467 fprintf(stderr, " Here -i 127.0.0.1 selects the ip address for the next -p 6969 and -P 6969.\n");
468 fprintf(stderr, " If no port is bound from config file or command line, the last address given\n");
469 fprintf(stderr, " is used on port 6969, and every interface if none was given.\n");
470}
471#undef HELPLINE
472
473
474static void listen_resolve_defaults(void) {
475 size_t i;
476
477 for (i = 0; i < g_listen_count; ++i) {
478 ot_listen *entry = g_listen + i;
479
480 if (entry->shared == OT_LISTEN_INHERIT)
481 entry->shared = g_listen_shared;
482 if (entry->workers == OT_LISTEN_INHERIT)
483 entry->workers = entry->proto == FLAG_TCP ? g_listen_workers_tcp : g_listen_workers_udp;
484
485 /* A tcp worker hands a connection it cannot finish to the event loop,
486 which only one thread may touch where the set is ours rather than the
487 kernel's */
488 if (entry->proto == FLAG_TCP && entry->workers > 0 && !event_can_share()) {
489 fprintf(stderr, "Warning: tcp workers impossible on this platform with event mechanism %s chosen.\n",
490 event_mechanism());
491 entry->workers = 0;
492 }
493 }
494}
495
496ot_listen *config_listen_entries(size_t *count) {
497 *count = g_listen_count;
498 return g_listen;
499}
500
501void config_parse(int argc, char **argv) {
502 ot_ip6 serverip;
503 ot_net tmpnet;
504 int bound = 0, scanon = 1;
505 uint64_t value;
506
507 memset(serverip, 0, sizeof(ot_ip6));
508#ifdef WANT_V4_ONLY
509 serverip[10] = serverip[11] = -1;
510#endif
511
512 while (scanon) {
513 switch (getopt(argc, argv,
514 ":i:p:A:P:d:u:r:s:f:l:v"
515#ifdef WANT_ACCESSLIST_BLACK
516 "b:"
517#elif defined(WANT_ACCESSLIST_WHITE)
518 "w:"
519#endif
520 "h")) {
521 case -1:
522 scanon = 0;
523 break;
524 case 'i':
525 if (!ip_parse(optarg, serverip)) {
526 usage(argv[0]);
527 exit(1);
528 }
529 break;
530#ifdef WANT_ACCESSLIST_BLACK
531 case 'b':
532 set_config_option(&g_accesslist_filename, optarg);
533 break;
534#elif defined(WANT_ACCESSLIST_WHITE)
535 case 'w':
536 set_config_option(&g_accesslist_filename, optarg);
537 break;
538#endif
539 case 'p':
540 if (!scan_uint(optarg, &value, 65535)) {
541 usage(argv[0]);
542 exit(1);
543 }
544 listen_append(serverip, (uint16_t)value, FLAG_TCP, OT_LISTEN_INHERIT, OT_LISTEN_INHERIT);
545 bound++;
546 break;
547 case 'P':
548 if (!scan_uint(optarg, &value, 65535)) {
549 usage(argv[0]);
550 exit(1);
551 }
552 listen_append(serverip, (uint16_t)value, FLAG_UDP, OT_LISTEN_INHERIT, OT_LISTEN_INHERIT);
553 bound++;
554 break;
555#ifdef WANT_SYNC_LIVE
556 case 's':
557 if (!scan_uint(optarg, &value, 65535)) {
558 usage(argv[0]);
559 exit(1);
560 }
561 listen_append(serverip, (uint16_t)value, FLAG_MCA, OT_LISTEN_INHERIT, OT_LISTEN_INHERIT);
562 break;
563#endif
564 case 'd':
565 set_config_option(&g_serverdir, optarg);
566 break;
567 case 'u':
568 set_config_option(&g_serveruser, optarg);
569 break;
570 case 'r':
571 set_config_option(&g_redirecturl, optarg);
572 break;
573 case 'l':
574 g_statefile = optarg;
575 break;
576 case 'A':
577 if (!scan_ip6_net(optarg, &tmpnet)) {
578 usage(argv[0]);
579 exit(1);
580 }
581 accesslist_bless_net(&tmpnet, 0xffff); /* Allow everything for now */
582 break;
583 case 'f':
584 bound += parse_configfile(optarg);
585 break;
586 case 'h':
587 help(argv[0]);
588 exit(0);
589 case 'v': {
590 char buffer[8192];
591 char *cursor = buffer;
592
593 stats_return_tracker_version(&cursor, buffer + sizeof(buffer) - 1);
594 stats_return_tracker_features(&cursor, buffer + sizeof(buffer) - 1);
595 *cursor = 0;
596 fputs(buffer, stderr);
597 exit(0);
598 }
599 default:
600 case '?':
601 usage(argv[0]);
602 exit(1);
603 }
604 }
605
606 if (!bound) {
607 listen_append(serverip, 6969, FLAG_TCP, OT_LISTEN_INHERIT, OT_LISTEN_INHERIT);
608 listen_append(serverip, 6969, FLAG_UDP, OT_LISTEN_INHERIT, OT_LISTEN_INHERIT);
609 }
610
611 listen_resolve_defaults();
612}