summaryrefslogtreecommitdiffstats
path: root/src/config.c
blob: ec04726961349456fd4be94f39c8991680793920 (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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
  Copyright (c) 2012-2013, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "fastd.h"
#include "peer.h"
#include <config.ll.h>
#include <config.yy.h>

#include <dirent.h>
#include <grp.h>
#include <libgen.h>
#include <pwd.h>
#include <stdarg.h>
#include <strings.h>

#include <arpa/inet.h>

#include <sys/stat.h>
#include <sys/types.h>


extern const fastd_protocol_t fastd_protocol_ec25519_fhmqvc;

extern const fastd_method_t fastd_method_null;

#ifdef WITH_METHOD_XSALSA20_POLY1305
extern const fastd_method_t fastd_method_xsalsa20_poly1305;
#endif
#ifdef WITH_METHOD_AES128_GCM
extern const fastd_method_t fastd_method_aes128_gcm;
#endif


#ifdef USE_CRYPTO_AES128CTR
#ifdef WITH_CRYPTO_AES128CTR_NACL
extern const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_nacl;
#endif
#ifdef WITH_CRYPTO_AES128CTR_LINUX
extern const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_linux;
#endif

#ifdef WITH_CRYPTO_AES128CTR_NACL
static const fastd_crypto_aes128ctr_t *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_nacl;
#else
static const fastd_crypto_aes128ctr_t *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_linux;
#endif

#endif

#ifdef USE_CRYPTO_GHASH
#ifdef WITH_CRYPTO_GHASH_BUILTIN
extern const fastd_crypto_ghash_t fastd_crypto_ghash_builtin;
#endif
#ifdef WITH_CRYPTO_GHASH_LINUX
extern const fastd_crypto_ghash_t fastd_crypto_ghash_linux;
#endif

#ifdef WITH_CRYPTO_GHASH_BUILTIN
static const fastd_crypto_ghash_t *fastd_crypto_ghash_default = &fastd_crypto_ghash_builtin;
#else
static const fastd_crypto_ghash_t *fastd_crypto_ghash_default = &fastd_crypto_ghash_linux;
#endif

#endif

static void default_config(fastd_config_t *conf) {
	memset(conf, 0, sizeof(fastd_config_t));

	conf->log_stderr_level = -1;
	conf->log_syslog_level = -1;
	conf->log_syslog_ident = strdup("fastd");

	conf->keepalive_interval = 20;
	conf->peer_stale_time = 90;
	conf->eth_addr_stale_time = 300;

	conf->reorder_count = 64;
	conf->reorder_time = 10;

	conf->min_handshake_interval = 15;
	conf->min_resolve_interval = 15;

	conf->mtu = 1500;
	conf->mode = MODE_TAP;

	conf->drop_caps = DROP_CAPS_ON;

	conf->protocol = &fastd_protocol_ec25519_fhmqvc;
	conf->method_default = &fastd_method_null;
	conf->key_valid = 3600;		/* 60 minutes */
	conf->key_refresh = 3300;	/* 55 minutes */
	conf->key_refresh_splay = 300;	/* 5 minutes */

#ifdef USE_CRYPTO_AES128CTR
	conf->crypto_aes128ctr = fastd_crypto_aes128ctr_default;
#endif
#ifdef USE_CRYPTO_GHASH
	conf->crypto_ghash = fastd_crypto_ghash_default;
#endif

	conf->peer_group = calloc(1, sizeof(fastd_peer_group_config_t));
	conf->peer_group->name = strdup("default");
	conf->peer_group->max_connections = -1;
}

static bool config_match(const char *opt, ...) {
	va_list ap;
	bool match = false;
	const char *str;

	va_start(ap, opt);

	while((str = va_arg(ap, const char*)) != NULL) {
		if (strcmp(opt, str) == 0) {
			match = true;
			break;
		}
	}

	va_end(ap);

	return match;
}

bool fastd_config_protocol(fastd_context_t *ctx, fastd_config_t *conf, const char *name) {
	if (!strcmp(name, "ec25519-fhmqvc"))
		conf->protocol = &fastd_protocol_ec25519_fhmqvc;
	else
		return false;

	return true;
}

static inline const fastd_method_t* parse_method_name(const char *name) {
	if (!strcmp(name, "null"))
		return &fastd_method_null;
#ifdef WITH_METHOD_XSALSA20_POLY1305
	else if (!strcmp(name, "xsalsa20-poly1305"))
		return &fastd_method_xsalsa20_poly1305;
#endif
#ifdef WITH_METHOD_AES128_GCM
	else if (!strcmp(name, "aes128-gcm"))
		return &fastd_method_aes128_gcm;
#endif
	else
		return NULL;
}

bool fastd_config_method(fastd_context_t *ctx, fastd_config_t *conf, const char *name) {
	const fastd_method_t *method = parse_method_name(name);

	if (!method)
		return false;

	conf->method_default = method;

	int i;
	for (i = 0; i < MAX_METHODS; i++) {
		if (conf->methods[i] == method)
			return true;

		if (conf->methods[i] == NULL) {
			conf->methods[i] = method;
			return true;
		}
	}

	exit_bug(ctx, "MAX_METHODS too low");
}

bool fastd_config_crypto(fastd_context_t *ctx, fastd_config_t *conf, const char *alg, const char *impl) {
#ifdef USE_CRYPTO_AES128CTR
	if (!strcasecmp(alg, "aes128-ctr") || !strcasecmp(alg, "aes128") || !strcasecmp(alg, "aes-ctr") || !strcasecmp(alg, "aes")) {
		if (!strcasecmp(impl, "default"))
			conf->crypto_aes128ctr = fastd_crypto_aes128ctr_default;
#ifdef WITH_CRYPTO_AES128CTR_NACL
		else if (!strcasecmp(impl, "nacl"))
			conf->crypto_aes128ctr = &fastd_crypto_aes128ctr_nacl;
#endif
#ifdef WITH_CRYPTO_AES128CTR_LINUX
		else if (!strcasecmp(impl, "linux"))
			conf->crypto_aes128ctr = &fastd_crypto_aes128ctr_linux;
#endif
		else
			return false;

		return true;
	}
	else
#endif
#ifdef USE_CRYPTO_GHASH
	if (!strcasecmp(alg, "ghash")) {
		if (!strcasecmp(impl, "default"))
			conf->crypto_ghash = fastd_crypto_ghash_default;
#ifdef WITH_CRYPTO_GHASH_BUILTIN
		else if (!strcasecmp(impl, "builtin"))
			conf->crypto_ghash = &fastd_crypto_ghash_builtin;
#endif
#ifdef WITH_CRYPTO_GHASH_LINUX
		else if (!strcasecmp(impl, "linux"))
			conf->crypto_ghash = &fastd_crypto_ghash_linux;
#endif
		else
			return false;

		return true;
	}
	else
#endif
	return false;
}

void fastd_config_bind_address(fastd_context_t *ctx, fastd_config_t *conf, const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6) {
	fastd_bind_address_t *addr = malloc(sizeof(fastd_bind_address_t));
	addr->next = conf->bind_addrs;
	conf->bind_addrs = addr;
	conf->n_bind_addrs++;

	addr->addr = *address;
	addr->bindtodev = bindtodev ? strdup(bindtodev) : NULL;

	fastd_peer_address_simplify(&addr->addr);

	if (addr->addr.sa.sa_family != AF_INET6 && (default_v4 || !conf->bind_addr_default_v4))
		conf->bind_addr_default_v4 = addr;

	if (addr->addr.sa.sa_family != AF_INET && (default_v6 || !conf->bind_addr_default_v6))
		conf->bind_addr_default_v6 = addr;
}

void fastd_config_peer_group_push(fastd_context_t *ctx, fastd_config_t *conf, const char *name) {
	fastd_peer_group_config_t *group = calloc(1, sizeof(fastd_peer_group_config_t));
	group->name = strdup(name);
	group->max_connections = -1;

	group->parent = conf->peer_group;
	group->next = group->parent->children;

	group->parent->children = group;

	conf->peer_group = group;
}

void fastd_config_peer_group_pop(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->peer_group = conf->peer_group->parent;
}

static void free_peer_group(fastd_peer_group_config_t *group) {
	while (group->children) {
		fastd_peer_group_config_t *next = group->children->next;
		free_peer_group(group->children);
		group->children = next;
	}

	fastd_string_stack_free(group->peer_dirs);
	free(group->name);
	free(group);
}

static bool has_peer_group_peer_dirs(const fastd_peer_group_config_t *group) {
	if (group->peer_dirs)
		return true;

	const fastd_peer_group_config_t *child;
	for (child = group->children; child; child = child->next) {
		if (has_peer_group_peer_dirs(child))
			return true;
	}

	return false;
}

bool fastd_config_add_log_file(fastd_context_t *ctx, fastd_config_t *conf, const char *name, int level) {
	char *name2 = strdup(name);
	char *name3 = strdup(name);

	char *dir = dirname(name2);
	char *base = basename(name3);

	char *oldcwd = get_current_dir_name();

	if (!chdir(dir)) {
		char *logdir = get_current_dir_name();

		fastd_log_file_t *file = malloc(sizeof(fastd_log_file_t));
		file->filename = malloc(strlen(logdir) + 1 + strlen(base) + 1);

		strcpy(file->filename, logdir);
		strcat(file->filename, "/");
		strcat(file->filename, base);

		file->level = level;

		file->next = conf->log_files;
		conf->log_files = file;

		if(chdir(oldcwd))
			pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno));

		free(logdir);
	}
	else {
		pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno));
	}

	free(oldcwd);
	free(name2);
	free(name3);

	return true;
}

static void read_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *dir) {
	DIR *dirh = opendir(".");

	if (dirh) {
		while (true) {
			struct dirent entry, *result;
			int ret;

			ret = readdir_r(dirh, &entry, &result);
			if (ret) {
				pr_error(ctx, "readdir_r: %s", strerror(ret));
				break;
			}

			if (!result)
				break;
			if (result->d_name[0] == '.')
				continue;

			if (result->d_name[strlen(result->d_name)-1] == '~') {
				pr_verbose(ctx, "ignoring file `%s' as it seems to be a backup file", result->d_name);
				continue;
			}

			struct stat statbuf;
			if (stat(result->d_name, &statbuf)) {
				pr_warn(ctx, "ignoring file `%s': stat failed: %s", result->d_name, strerror(errno));
				continue;
			}
			if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
				pr_info(ctx, "ignoring file `%s': no regular file", result->d_name);
				continue;
			}

			fastd_peer_config_new(ctx, conf);
			conf->peers->name = strdup(result->d_name);
			conf->peers->config_source_dir = dir;

			if (!fastd_read_config(ctx, conf, result->d_name, true, 0)) {
				pr_warn(ctx, "peer config `%s' will be ignored", result->d_name);
				fastd_peer_config_delete(ctx, conf);
			}
		}

		closedir(dirh);
	}
	else {
		pr_error(ctx, "opendir for `%s' failed: %s", dir, strerror(errno));
	}
}

static void read_peer_dirs(fastd_context_t *ctx, fastd_config_t *conf) {
	char *oldcwd = get_current_dir_name();

	fastd_string_stack_t *dir;
	for (dir = conf->peer_group->peer_dirs; dir; dir = dir->next) {
		if (!chdir(dir->str))
			read_peer_dir(ctx, conf, dir->str);
		else
			pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir->str, strerror(errno));
	}

	if (chdir(oldcwd))
		pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno));

	free(oldcwd);
}

void fastd_add_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *dir) {
	char *oldcwd = get_current_dir_name();

	if (!chdir(dir)) {
		char *newdir = get_current_dir_name();
		conf->peer_group->peer_dirs = fastd_string_stack_push(conf->peer_group->peer_dirs, newdir);
		free(newdir);

		if(chdir(oldcwd))
			pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno));
	}
	else {
		pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno));
	}

	free(oldcwd);
}

bool fastd_read_config(fastd_context_t *ctx, fastd_config_t *conf, const char *filename, bool peer_config, int depth) {
	if (depth >= MAX_CONFIG_DEPTH)
		exit_error(ctx, "maximum config include depth exceeded");

	bool ret = true;
	char *oldcwd = get_current_dir_name();
	char *filename2 = NULL;
	char *dir = NULL;
	FILE *file;
	yyscan_t scanner;
	fastd_config_pstate *ps;
	fastd_string_stack_t *strings = NULL;

	fastd_config_yylex_init(&scanner);
	ps = fastd_config_pstate_new();

	if (!filename) {
		file = stdin;
	}
	else {
		file = fopen(filename, "r");
		if (!file) {
			pr_error(ctx, "can't open config file `%s': %s", filename, strerror(errno));
			ret = false;
			goto end_free;
		}
	}

	fastd_config_yyset_in(file, scanner);

	if (filename) {
		filename2 = strdup(filename);
		dir = dirname(filename2);

		if (chdir(dir)) {
			pr_error(ctx, "change from directory `%s' to `%s' failed", oldcwd, dir);
			ret = false;
			goto end_free;
		}
	}

	int token;
	YYSTYPE token_val;
	YYLTYPE loc = {1, 0, 1, 0};

	if (peer_config)
		token = START_PEER_CONFIG;
	else
		token = conf->peer_group->parent ? START_PEER_GROUP_CONFIG : START_CONFIG;

	int parse_ret = fastd_config_push_parse(ps, token, &token_val, &loc, ctx, conf, filename, depth+1);

	while(parse_ret == YYPUSH_MORE) {
		token = fastd_config_yylex(&token_val, &loc, scanner);

		if (token < 0) {
			pr_error(ctx, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column);
			ret = false;
			goto end_free;
		}

		if (token == TOK_STRING) {
			token_val.str->next = strings;
			strings = token_val.str;
		}

		parse_ret = fastd_config_push_parse(ps, token, &token_val, &loc, ctx, conf, filename, depth+1);
	}

	if (parse_ret)
		ret = false;

 end_free:
	fastd_string_stack_free(strings);

	fastd_config_pstate_delete(ps);
	fastd_config_yylex_destroy(scanner);

	if(chdir(oldcwd))
		pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno));

	free(filename2);
	free(oldcwd);

	if (filename && file)
		fclose(file);

	return ret;
}

static void assess_peers(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->has_floating = false;

	fastd_peer_config_t *peer;
	for (peer = conf->peers; peer; peer = peer->next) {
		if (fastd_peer_config_is_floating(peer))
			conf->has_floating = true;

		if (peer->dynamic_float_deprecated)
			pr_warn(ctx, "peer `%s' uses deprecated float syntax, please update your configuration", peer->name);
	}
}

static void print_usage(const char *options, const char *message) {
	/* 28 spaces */
	static const char spaces[] = "                            ";

	int len = strlen(options);

	printf("%s", options);

	if (len < 28)
		printf("%s", spaces+len);
	else
		printf("\n%s", spaces);

	puts(message);
}

static void usage(fastd_context_t *ctx, fastd_config_t *conf) {
	puts("fastd (Fast and Secure Tunnelling Daemon) " FASTD_VERSION " usage:\n");

#define OR ", "
#define OPTION(func, options, message) print_usage("  " options, message)
#define OPTION_ARG(func, options, arg, message) print_usage("  " options " " arg, message)
#include "options.def.h"
#undef OR
#undef OPTION
#undef OPTION_ARG

	exit(0);
}

static void version(fastd_context_t *ctx, fastd_config_t *conf) {
	puts("fastd " FASTD_VERSION);
	exit(0);
}

static int parse_log_level(fastd_context_t *ctx, const char *arg) {
	if (!strcmp(arg, "fatal"))
		return LOG_CRIT;
	else if (!strcmp(arg, "error"))
		return LOG_ERR;
	else if (!strcmp(arg, "warn"))
		return LOG_WARNING;
	else if (!strcmp(arg, "info"))
		return LOG_NOTICE;
	else if (!strcmp(arg, "verbose"))
		return LOG_INFO;
	else if (!strcmp(arg, "debug"))
		return LOG_DEBUG;
	else
		exit_error(ctx, "invalid log level `%s'", arg);
}



static void option_user(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->user);
	conf->user = strdup(arg);
}

static void option_group(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->group);
	conf->group = strdup(arg);
}

static void option_log_level(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	conf->log_stderr_level = parse_log_level(ctx, arg);
}

static void option_syslog_level(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	conf->log_syslog_level = parse_log_level(ctx, arg);
}

static void option_syslog_ident(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->log_syslog_ident);
	conf->log_syslog_ident = strdup(arg);
}

static void option_hide_ip_addresses(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->hide_ip_addresses = true;
}

static void option_hide_mac_addresses(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->hide_mac_addresses = true;
}

static void option_config(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	if (!strcmp(arg, "-"))
		arg = NULL;

	if (!fastd_read_config(ctx, conf, arg, false, 0))
		exit(1);
}

static void option_config_peer(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	fastd_peer_config_new(ctx, conf);

	if(!fastd_read_config(ctx, conf, arg, true, 0))
		exit(1);
}

static void option_config_peer_dir(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	fastd_add_peer_dir(ctx, conf, arg);
}

static void option_mode(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	if (!strcmp(arg, "tap"))
		conf->mode = MODE_TAP;
	else if (!strcmp(arg, "tun"))
		conf->mode = MODE_TUN;
	else
		exit_error(ctx, "invalid mode `%s'", arg);
}

static void option_interface(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->ifname);
	conf->ifname = strdup(arg);
}

static void option_mtu(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	char *endptr;

	conf->mtu = strtol(arg, &endptr, 10);
	if (*endptr || conf->mtu < 576)
		exit_error(ctx, "invalid mtu `%s'", arg);
}

static void option_bind(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	long l;
	char *charptr;
	char *endptr;
	char *addrstr;

	if (arg[0] == '[') {
		charptr = strchr(arg, ']');
		if (!charptr || (charptr[1] != ':' && charptr[1] != '\0'))
			exit_error(ctx, "invalid bind address `%s'", arg);

		addrstr = strndup(arg+1, charptr-arg-1);

		if (charptr[1] == ':')
			charptr++;
		else
			charptr = NULL;
	}
	else {
		charptr = strchr(arg, ':');
		if (charptr) {
			addrstr = strndup(arg, charptr-arg);
		}
		else {
			addrstr = strdup(arg);
		}
	}

	if (charptr) {
		l = strtol(charptr+1, &endptr, 10);
		if (*endptr || l < 0 || l > 65535)
			exit_error(ctx, "invalid bind port `%s'", charptr+1);
	}
	else {
		l = 0;
	}

	fastd_peer_address_t addr = {};

	if (strcmp(addrstr, "any") == 0) {
		/* nothing to do */
	}
	else if (arg[0] == '[') {
		addr.in6.sin6_family = AF_INET6;
		addr.in6.sin6_port = htons(l);

		if (inet_pton(AF_INET6, addrstr, &addr.in6.sin6_addr) != 1)
			exit_error(ctx, "invalid bind address `%s'", addrstr);
	}
	else {
		addr.in.sin_family = AF_INET;
		addr.in.sin_port = htons(l);

		if (inet_pton(AF_INET, addrstr, &addr.in.sin_addr) != 1)
			exit_error(ctx, "invalid bind address `%s'", addrstr);
	}

	free(addrstr);

	fastd_config_bind_address(ctx, conf, &addr, NULL, false, false);
}

static void option_protocol(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	if (!fastd_config_protocol(ctx, conf, arg))
		exit_error(ctx, "invalid protocol `%s'", arg);
}

static void option_method(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	if (!fastd_config_method(ctx, conf, arg))
		exit_error(ctx, "invalid method `%s'", arg);
}

static void option_forward(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->forward = true;
}

static void option_on_up(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->on_up);
	free(conf->on_up_dir);

	conf->on_up = strdup(arg);
	conf->on_up_dir = get_current_dir_name();
}

static void option_on_down(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->on_down);
	free(conf->on_down_dir);

	conf->on_down = strdup(arg);
	conf->on_down_dir = get_current_dir_name();
}

static void option_on_establish(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->on_establish);
	free(conf->on_establish_dir);

	conf->on_establish = strdup(arg);
	conf->on_establish_dir = get_current_dir_name();
}

static void option_on_disestablish(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->on_disestablish);
	free(conf->on_disestablish_dir);

	conf->on_disestablish = strdup(arg);
	conf->on_disestablish_dir = get_current_dir_name();
}

static void option_on_verify(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->on_verify);
	free(conf->on_verify_dir);

	conf->on_verify = strdup(arg);
	conf->on_verify_dir = get_current_dir_name();
}

static void option_daemon(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->daemon = true;
}

static void option_pid_file(fastd_context_t *ctx, fastd_config_t *conf, const char *arg) {
	free(conf->pid_file);
	conf->pid_file = strdup(arg);
}

static void option_generate_key(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->generate_key = true;
	conf->show_key = false;
}

static void option_show_key(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->generate_key = false;
	conf->show_key = true;
}

static void option_machine_readable(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->machine_readable = true;
}


static void configure_user(fastd_context_t *ctx, fastd_config_t *conf) {
	conf->uid = getuid();
	conf->gid = getgid();

	if (conf->user) {
		struct passwd pwd, *pwdr;
		size_t bufspace = 1024;
		int error;

		do {
			char buf[bufspace];
			error = getpwnam_r(conf->user, &pwd, buf, bufspace, &pwdr);
			bufspace *= 2;
		} while(error == ERANGE);

		if (error)
			exit_errno(ctx, "getpwnam_r");

		if (!pwdr)
			exit_error(ctx, "Unable to find user `%s'.", conf->user);

		conf->uid = pwdr->pw_uid;
		conf->gid = pwdr->pw_gid;
	}

	if (conf->group) {
		struct group grp, *grpr;
		size_t bufspace = 1024;
		int error;

		do {
			char buf[bufspace];
			error = getgrnam_r(conf->group, &grp, buf, bufspace, &grpr);
			bufspace *= 2;
		} while(error == ERANGE);

		if (error)
			exit_errno(ctx, "getgrnam_r");

		if (!grpr)
			exit_error(ctx, "Unable to find group `%s'.", conf->group);

		conf->gid = grpr->gr_gid;
	}

	if (conf->user) {
		int ngroups = 0;
		if (getgrouplist(conf->user, conf->gid, NULL, &ngroups) < 0) {
			/* the user has supplementary groups */

			conf->groups = calloc(ngroups, sizeof(gid_t));
			if (getgrouplist(conf->user, conf->gid, conf->groups, &ngroups) < 0)
				exit_errno(ctx, "getgrouplist");

			conf->n_groups = ngroups;
		}
	}

}

void fastd_configure(fastd_context_t *ctx, fastd_config_t *conf, int argc, char *const argv[]) {
	default_config(conf);

	int i = 1;
	while (i < argc) {
#define OR ,
#define OPTION(func, options, message)					\
		({							\
			if(config_match(argv[i], options, NULL)) {	\
				i++;					\
				func(ctx, conf);			\
				continue;				\
			}						\
		})
#define OPTION_ARG(func, options, arg, message)				\
		({							\
			if(config_match(argv[i], options, NULL)) {	\
				i+=2;					\
				if (i > argc)				\
					exit_error(ctx, "config error: option `%s' needs an argument; see --help for usage", argv[i-2]); \
				func(ctx, conf, argv[i-1]);		\
				continue;				\
			}						\
		})
#include "options.def.h"
#undef OR
#undef OPTION
#undef OPTION_ARG

		exit_error(ctx, "config error: unknown option `%s'; see --help for usage", argv[i]);
	}

	if (conf->log_stderr_level < 0 && conf->log_syslog_level < 0 && !conf->log_files)
		conf->log_stderr_level = FASTD_DEFAULT_LOG_LEVEL;

	if (!conf->methods[0])
		conf->methods[0] = conf->method_default;

	if (conf->generate_key || conf->show_key)
		return;

	if (conf->mode == MODE_TUN) {
		if (!conf->peers || conf->peers->next)
			exit_error(ctx, "config error: in TUN mode exactly one peer must be configured");
		if (conf->peer_group->children)
			exit_error(ctx, "config error: in TUN mode peer groups can't be used");
		if (has_peer_group_peer_dirs(conf->peer_group))
			exit_error(ctx, "config error: in TUN mode peer directories can't be used");
	}

	if (!conf->peers && !has_peer_group_peer_dirs(conf->peer_group))
		exit_error(ctx, "config error: neither fixed peers nor peer dirs have been configured");

	configure_user(ctx, conf);
}

static void peer_dirs_read_peer_group(fastd_context_t *ctx, fastd_config_t *new_conf) {
	read_peer_dirs(ctx, new_conf);

	fastd_peer_group_config_t *group;
	for (group = new_conf->peer_group->children; group; group = group->next) {
		new_conf->peer_group = group;
		peer_dirs_read_peer_group(ctx, new_conf);
	}
}

static void peer_dirs_handle_old_peers(fastd_context_t *ctx, fastd_peer_config_t **old_peers, fastd_peer_config_t **new_peers) {
	fastd_peer_config_t **peer, **next, **new_peer, **new_next;
	for (peer = old_peers; *peer; peer = next) {
		next = &(*peer)->next;

		/* don't touch statically configured peers */
		if (!(*peer)->config_source_dir)
			continue;

		/* search for each peer in the list of new peers */
		for (new_peer = new_peers; *new_peer; new_peer = new_next) {
			new_next = &(*new_peer)->next;

			if (((*peer)->config_source_dir == (*new_peer)->config_source_dir) && strequal((*peer)->name, (*new_peer)->name)) {
				if (fastd_peer_config_equal(*peer, *new_peer)) {
					pr_verbose(ctx, "peer `%s' unchanged", (*peer)->name);

					fastd_peer_config_t *free_peer = *new_peer;
					*new_peer = *new_next;
					fastd_peer_config_free(free_peer);
					peer = NULL;
				}
				else {
					pr_verbose(ctx, "peer `%s' changed, resetting", (*peer)->name);
					new_peer = NULL;
				}

				break;
			}
		}

		/* no new peer was found, or the old one has changed */
		if (peer && (!new_peer || !*new_peer)) {
			pr_verbose(ctx, "removing peer `%s'", (*peer)->name);

			fastd_peer_config_t *free_peer = *peer;
			*peer = *next;
			next = peer;

			fastd_peer_config_purge(ctx, free_peer);
		}
	}
}

static void peer_dirs_handle_new_peers(fastd_context_t *ctx, fastd_peer_config_t **peers, fastd_peer_config_t *new_peers) {
	fastd_peer_config_t *peer;
	for (peer = new_peers; peer; peer = peer->next) {
		if (peer->next)
			continue;

		peer->next = *peers;
		*peers = new_peers;
		return;
	}
}

void fastd_config_load_peer_dirs(fastd_context_t *ctx, fastd_config_t *conf) {
	fastd_config_t temp_conf;
	temp_conf.peer_group = conf->peer_group;
	temp_conf.peers = NULL;

	peer_dirs_read_peer_group(ctx, &temp_conf);
	peer_dirs_handle_old_peers(ctx, &conf->peers, &temp_conf.peers);
	peer_dirs_handle_new_peers(ctx, &conf->peers, temp_conf.peers);

	assess_peers(ctx, conf);
}

void fastd_config_release(fastd_context_t *ctx, fastd_config_t *conf) {
	while (conf->peers)
		fastd_peer_config_delete(ctx, conf);

	while (conf->log_files) {
		fastd_log_file_t *next = conf->log_files->next;
		free(conf->log_files->filename);
		free(conf->log_files);
		conf->log_files = next;
	}

	while (conf->bind_addrs) {
		fastd_bind_address_t *next = conf->bind_addrs->next;
		free(conf->bind_addrs->bindtodev);
		free(conf->bind_addrs);
		conf->bind_addrs = next;
	}

	free_peer_group(conf->peer_group);

	free(conf->user);
	free(conf->group);
	free(conf->groups);
	free(conf->ifname);
	free(conf->secret);
	free(conf->on_up);
	free(conf->on_up_dir);
	free(conf->on_down);
	free(conf->on_down_dir);
	free(conf->on_establish);
	free(conf->on_establish_dir);
	free(conf->on_disestablish);
	free(conf->on_disestablish_dir);
	free(conf->on_verify);
	free(conf->on_verify_dir);
	free(conf->protocol_config);
	free(conf->log_syslog_ident);
}