This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseconfig.c
1853 lines (1615 loc) · 43.9 KB
/
parseconfig.c
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
/*
* Copyright (c) 2018, 2019, 2020 Tim Kuijsten
*
* Permission to use, copy, modify, and distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright
* notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/socket.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <openssl/curve25519.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "base64.h"
#include "util.h"
#include "wiresep.h"
#include "parseconfig.h"
#define B64KEYLEN 44
#define MAXLISTEN6 40
#define MAXLISTEN4 40
/* global settings */
static char *guser, *ggroup, *gpskfile;
static wskey gpsk;
static uid_t guid;
static gid_t ggid;
static const char *logfacilitystr = "daemon";
static int logfacility;
static const wskey nullkey;
static const wskey basepoint = {9};
static struct cfgifn **ifnv;
static size_t ifnvsize;
/*
* Create a new "member" of size "membersize" and add it to "arr". Exit on
* failure.
*/
static void
xaddone(void ***arr, size_t *curmemcount, void **member, size_t membersize)
{
*arr = recallocarray(*arr, *curmemcount, *curmemcount + 1,
sizeof(char *));
if (*arr == NULL)
err(1, "parseconfig %s recallocarray error", __func__);
if ((*member = calloc(1, membersize)) == NULL)
err(1, "parseconfig %s calloc error", __func__);
(*arr)[*curmemcount] = *member;
(*curmemcount)++;
}
/*
* Parse an IPv4 or IPv6 address with port.
*
* Return 0 on success, -1 on error.
*/
static int
parseaddrport(struct sockaddr_storage *addr, const char *str)
{
char *a, *colon, *s;
if ((s = strdup(str)) == NULL)
err(1, "parseconfig %s strdup error", __func__);
if ((colon = strrchr(s, ':')) == NULL)
goto err;
*colon = '\0';
if (strcmp(s, "*") == 0) {
a = NULL;
} else {
a = s;
}
/* remove brackets */
if (a && *a == '[') {
if (*(colon - 1) != ']')
goto err;
a++;
*(colon - 1) = '\0';
}
if (strtoaddr(addr, a, colon + 1, AI_PASSIVE) != 0)
goto err;
free(s);
s = NULL;
return 0;
err:
free(s);
s = NULL;
return -1;
}
/*
* Parse an IPv4 or IPv6 address in CIDR notation.
*
* Return 0 on success, -1 on error.
*/
static int
parseipcidr(struct sockaddr_storage *ip, size_t *prefixlen, const char *str)
{
const char *errstr;
char *slash, *s;
if ((s = strdup(str)) == NULL)
err(1, "parseconfig %s strdup error", __func__);
slash = strchr(s, '/');
if (slash) {
*slash = '\0';
*prefixlen = strtonum(slash + 1, 0, 128, &errstr);
if (errstr != NULL)
goto err;
}
if (strtoaddr(ip, s, NULL, AI_NUMERICHOST | AI_PASSIVE) != 0)
goto err;
if (!slash) {
/* use default prefixlen */
if (ip->ss_family == AF_INET6) {
*prefixlen = 128;
} else if (ip->ss_family == AF_INET) {
*prefixlen = 32;
} else
goto err;
}
free(s);
s = NULL;
return 0;
err:
free(s);
s = NULL;
return -1;
}
/*
* Parse "path" for a base64 key and place it in "key".
*
* Empty lines or lines starting with a hash '#' are ignored. Also any text
* after a key is ignored.
*
* Returns 1 if a key is set, 0 if no key is found and -1 on error with errno
* set.
*/
static int
parsekeyfile(wskey key, const char *path)
{
char *line;
size_t len, lineno, s;
int rc;
FILE *fp;
s = 0;
rc = 0;
lineno = 0;
line = NULL;
if ((fp = fopen(path, "re")) == NULL) {
/* errno set */
warn("could not open key file %s", path);
return -1;
}
if (!isfdsafe(fileno(fp), 0600)) {
warnx("%s: must be owned by the superuser and may not be "
"readable or writable by the group or others", path);
rc = -1;
goto cleanup;
}
while (getline(&line, &s, fp) > 0) {
lineno++;
len = strcspn(line, "\n");
if (len == 0)
continue;
if (line[0] == '#')
continue;
line[len] = '\0';
if (len < B64KEYLEN) {
warnx("%s error on line %zu: illegal key", path,
lineno);
errno = EINVAL;
rc = -1;
goto cleanup;
}
if (len > B64KEYLEN && !isblank(line[B64KEYLEN])) {
warnx("%s error on line %zu: keys and comments must be "
"separated by a space or tab", path, lineno);
errno = EINVAL;
rc = -1;
goto cleanup;
}
/* ignore everything following the key */
line[B64KEYLEN] = '\0';
if (base64_pton(line, key, KEYLEN) == -1) {
errno = EINVAL;
rc = -1;
goto cleanup;
}
rc = 1;
goto cleanup;
}
cleanup:
free(line);
if (ferror(fp)) {
warn("error reading %s", path);
rc = -1;
}
if (fclose(fp) == EOF) {
warn("error closing %s", path);
rc = -1;
}
return rc;
}
/*
* Input must be a nul terminated Base64 or hexadecimal string containing a 256
* bit key.
*
* Return -1 on error.
*/
static int
parsekey(wskey dst, const char *src, size_t srcsize)
{
if (srcsize == 64) {
if (readhexnomem(dst, KEYLEN, src, 64) == -1)
return -1;
} else if (srcsize == 44) {
if (base64_pton(src, dst, KEYLEN) == -1)
return -1;
} else
return -1;
return 0;
}
/*
* Try to load a private or pre-shared key from a default path.
*
* If both "ifname" and "peername" are not NULL an interface bound peer specific
* per-shared key is tried.
* If "ifname" and "peername" are both NULL the default path for a global pre-
* shared key is tried.
* If "ifname" is NULL and "peername" is not NULL a peer specific pre-shared key
* is tried.
* If "peername" is NULL and "ifname" is not NULL an interface key path is
* tried. In this case "ext" indicates whether to try a private key or a pre-
* shared key by being either "privkey" or "psk", respectively.
*
* Returns 1 if a key was found and parsed into "out".
* Returns 0 if the default path existed and had no syntax errors, but alsno no
* key.
* Returns -1 if an error occurred while opening the default path, errno will be
* set to any error that open(2) can set.
* Returns -2 if parsekeyfile() returned an error, in this case errno will *not*
* be set.
* Returns -3 if "ext" is not one of NULL, "psk" or "privkey".
*
* Exits on asprintf(3) error.
*/
static int
xtrydefaultkey(wskey out, const char *ifname, const char *peername,
const char *ext)
{
char *defaultkeypath;
int d, rc;
if (ifname && peername) {
if (asprintf(&defaultkeypath, "/etc/wiresep/%s.%s.psk", ifname,
peername) < 0)
errx(1, "could not allocate default interface bound "
"peer specific key path");
} else if (ifname) {
if (ext == NULL)
return -3;
if (strcmp(ext, "psk") != 0 && strcmp(ext, "privkey") != 0)
return -3;
if (asprintf(&defaultkeypath, "/etc/wiresep/%s.%s", ifname, ext)
< 0)
errx(1, "could not allocate default interface specific "
"key path");
} else if (peername) {
if (asprintf(&defaultkeypath, "/etc/wiresep/%s.psk", peername)
< 0)
errx(1, "could not allocate default peer specific key "
"path");
} else {
if (asprintf(&defaultkeypath, "/etc/wiresep/global.psk") < 0)
errx(1, "could not allocate default peer specific key "
"path");
}
d = open(defaultkeypath, O_RDONLY);
if (d == -1) {
/* errno set */
free(defaultkeypath);
return -1;
} else {
close(d);
rc = parsekeyfile(out, defaultkeypath);
if (rc == 1) {
if (verbose > 0)
warnx("key loaded: %s", defaultkeypath);
} else {
warnx("could not load key: %s", defaultkeypath);
}
free(defaultkeypath);
if (rc == -1)
return -2;
return rc;
}
}
/*
* Get the number of the name of a tunnel interface.
*
* Returns the number of the tunnel interface on success, or -1 on failure.
*/
static int
gettunnum(const char *name)
{
int tunnum;
char c;
if (sscanf(name, "tun%d%c", &tunnum, &c) != 1)
return -1;
return tunnum;
}
/*
* Make sure the interface name is in the form tunDDD where DDD is one to three
* digits.
*
* Return 1 if valid, 0 if invalid.
*/
static int
validinterfacename(const char *name)
{
if (gettunnum(name) == -1)
return 0;
return 1;
}
/*
* Make sure the peer name contains no non-graphical characters, no '/' and
* does not constitute an interface name or the word "global".
*
* Return 1 if valid, 0 if invalid.
*/
static int
validpeername(const char *name)
{
size_t i, len;
len = strlen(name);
if (len == 0)
return 0;
if (strcasecmp(name, "global") == 0)
return 0;
if (validinterfacename(name))
return 0;
/* no path separators */
if (strchr(name, '/') != NULL)
return 0;
for (i = 0; i < len; i++)
if (!isgraph(name[i]))
return 0;
return 1;
}
/*
* Parse a peer config.
*
* Return 0 on success, -1 on error.
*/
static int
parsepeerconfig(struct cfgpeer *peer, const struct scfge *cfg, int peernumber,
const struct cfgifn *ifn)
{
struct cfgcidraddr *allowedip;
struct scfge *subcfg;
const char *key, *peerid;
size_t i, j;
int e, rc, explicitpeername;
if (cfg == NULL || cfg->strvsize < 1)
return -1;
if (strcasecmp("peer", cfg->strv[0]) != 0)
return -1;
explicitpeername = 0;
/*
* Use optional peer name or a default until the public key is
* encountered.
*/
if (cfg->strvsize >= 2) {
if (validpeername(cfg->strv[1])) {
strlcpy(peer->name, cfg->strv[1], sizeof(peer->name));
explicitpeername = 1;
} else {
warnx("%s: invalid peer name. Peer name may not be the "
"word \"global\", be an interface name, contain a "
"'/' or any non-graphical character", peer->name);
e = 1;
}
} else {
snprintf(peer->name, sizeof(peer->name), "peer%d", peernumber);
}
peerid = peer->name;
e = 0;
for (i = 0; i < cfg->entryvsize; i++) {
subcfg = cfg->entryv[i];
if (subcfg->strvsize < 1) {
assert(subcfg->entryvsize > 0);
warnx("%s: no blocks allowed in a peer block",
peerid);
e = 1;
continue;
}
key = subcfg->strv[0];
if (key == NULL) {
assert(subcfg->entryvsize > 0);
warnx("%s: peer block may not contain another "
"block", peerid);
e = 1;
continue;
}
if (subcfg->entryvsize > 0) {
warnx("%s: %s may not have a block associated",
peerid, key);
e = 1;
/* check other keywords */
}
if (strcasecmp("pubkey", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s missing public key", peerid,
key);
e = 1;
continue;
}
if (parsekey(peer->pubkey, subcfg->strv[1],
strlen(subcfg->strv[1])) == -1) {
warnx("peer public key must be Base64 or "
"hexadecimal");
return -1;
}
if (cfg->strvsize < 2) {
/* Use public key as peer name since the user
* did not provide a name
*/
strlcpy(peer->name, subcfg->strv[1],
sizeof(peer->name));
}
} else if (strcasecmp("endpoint", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must have an address and port "
"separated by a colon", peerid, key);
e = 1;
continue;
}
if (parseaddrport(&peer->fsa, subcfg->strv[1]) == -1) {
warnx("%s: %s parse error: %s. Make sure the "
"address and port are separated by a colon"
, peerid, key, subcfg->strv[1]);
e = 1;
continue;
}
} else if (strcasecmp("allowedips", key) == 0) {
if (subcfg->strvsize < 2) {
warnx("%s: %s must contain at least one "
"ip/mask", peerid, key);
e = 1;
continue;
}
for (j = 1; j < subcfg->strvsize; j++) {
if (strcmp(subcfg->strv[j], "*") == 0) {
xaddone((void ***)&peer->allowedips,
&peer->allowedipssize,
(void **)&allowedip,
sizeof(*allowedip));
if (parseipcidr(&allowedip->addr,
&allowedip->prefixlen, "0.0.0.0/0")
== -1)
abort();
xaddone((void ***)&peer->allowedips,
&peer->allowedipssize,
(void **)&allowedip,
sizeof(*allowedip));
if (parseipcidr(&allowedip->addr,
&allowedip->prefixlen, "::/0")
== -1)
abort();
} else {
xaddone((void ***)&peer->allowedips,
&peer->allowedipssize,
(void **)&allowedip,
sizeof(*allowedip));
if (parseipcidr(&allowedip->addr,
&allowedip->prefixlen,
subcfg->strv[j]) == -1) {
warnx("%s: %s could not parse "
"ip: %s", peerid, key,
subcfg->strv[j]);
e = 1;
continue;
}
}
}
} else if (strcasecmp("psk", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must have a value", peerid,
key);
e = 1;
continue;
}
if (parsekey(peer->psk, subcfg->strv[1],
strlen(subcfg->strv[1])) == -1) {
warnx("%s: %s could not parse pre-sharedkey",
peerid, key);
e = 1;
continue;
}
} else if (strcasecmp("pskfile", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s path missing", peerid, key);
e = 1;
continue;
}
if ((peer->pskfile = strdup(subcfg->strv[1])) == NULL)
err(1, "parseconfig strdup error peer pskfile");
} else {
warnx("%s: %s invalid keyword in peer scope", peerid,
key);
e = 1;
}
}
/*
* Ensure defaults.
*/
if (memcmp(peer->psk, nullkey, sizeof(wskey)) == 0) {
if (peer->pskfile != NULL) {
rc = parsekeyfile(peer->psk, peer->pskfile);
if (rc == -1) {
warnx("%s: could not parse peer pskfile: %s",
peerid, peer->pskfile);
e = 1;
} else if (rc == 0) {
warnx("%s: could not find a key in peer pskfile"
": %s", peerid, peer->pskfile);
e = 1;
}
} else if (explicitpeername) {
/*
* Look for an interface bound peer specific pre-shared
* key.
*/
rc = xtrydefaultkey(peer->psk, ifn->ifname, peer->name,
NULL);
if (rc == 1) {
/* key loaded! */
} else if ((rc == -1 && errno == ENOENT) || rc == 0) {
/*
* No key found, try a global peer specific key.
*/
rc = xtrydefaultkey(peer->psk, NULL, peer->name,
NULL);
if (rc == 1) {
/* key loaded! */
} else if (rc == 0) {
/* no key found, be silent */
} else if (rc == -1 && errno == ENOENT) {
/* no key found, be silent */
} else if (rc == -1) {
warn("could not load default peer "
"specific pre-shared key file: %s",
peer->name);
e = 1;
} else if (rc == -2) {
warnx("could not parse default peer "
"specific pre-shared key file: %s",
peer->name);
e = 1;
} else if (rc == -3) {
warnx("xtrydefaultkey peer pre-shared "
"key file error %s", peer->name);
e = 1;
}
} else if (rc == -1 && errno != ENOENT) {
warn("could not load default interface bound "
"peer specific pre-shared key file: %s %s",
ifn->ifname, peer->name);
e = 1;
} else if (rc == -2) {
warnx("could not parse default interface bound"
" peer specific pre-shared key file: %s %s",
ifn->ifname, peer->name);
e = 1;
} else if (rc == -3) {
warnx("%s: xtrydefaultkey peer pre-shared key "
"file error %s", ifn->ifname, peer->name);
e = 1;
}
}
}
/* if still no pre-shared key, try to use the default one */
if (memcmp(peer->psk, nullkey, sizeof(wskey)) == 0)
memcpy(peer->psk, ifn->psk, sizeof(wskey));
if (peer->allowedipssize == 0) {
warnx("%s: allowedips missing", peerid);
e = 1;
}
if (memcmp(peer->pubkey, nullkey, sizeof(wskey)) == 0) {
warnx("%s: pubkey missing", peerid);
e = 1;
}
if (e != 0)
return -1;
return 0;
}
/*
* Parse all interface configs. "ifnv" must be pre-allocated.
*
* In a first pass determine all interface settings and in a second pass
* parse all peer specific settings.
*
* Return 0 on success, -1 on error.
*/
static int
parseinterfaceconfigs(void)
{
struct scfge *subcfg;
struct cfgifn *ifn;
struct cfgpeer *peer;
struct cfgcidraddr *ifaddr;
struct sockaddr_storage listenaddr;
struct sockaddr_in6 *laddrs6;
struct sockaddr_in *laddrs4;
struct stat st;
const char *key;
char tundevpath[29];
size_t i, j, n;
int e, rc, tunnum;
e = 0;
for (n = 0; n < ifnvsize; n++) {
ifn = ifnv[n];
if (ifn->scfge->strvsize != 2) {
warnx("interface keyword must be followed by the"
" interface name and then a block");
e = 1;
continue;
}
assert(strcasecmp("interface", ifn->scfge->strv[0]) == 0);
tunnum = gettunnum(ifn->scfge->strv[1]);
if (tunnum == -1) {
warnx("interface name must be a device name like tun0 "
"or tun2: %s", ifn->scfge->strv[1]);
e = 1;
continue;
}
for (i = 0; i < n; i++) {
if (ifnv[i]->ifname &&
strcmp(ifn->scfge->strv[1], ifnv[i]->ifname) == 0) {
warnx("interface name is not unique: %s",
ifn->scfge->strv[1]);
e = 1;
continue;
}
}
if (snprintf(tundevpath, sizeof(tundevpath), "/dev/%s",
ifn->scfge->strv[1]) < 9)
errx(1, "snprintf error tundevpath");
if (stat(tundevpath, &st) == -1) {
warn("stat error %s", tundevpath);
e = 1;
continue;
}
if (st.st_uid != 0) {
warnx("%s: not owned by the superuser", tundevpath);
e = 1;
continue;
}
if (!S_ISCHR(st.st_mode)) {
warnx("%s: not a device file", tundevpath);
e = 1;
continue;
}
if ((ifn->ifname = strdup(ifn->scfge->strv[1])) == NULL)
err(1, "parseconfig strdup error ifname");
for (i = 0; i < ifn->scfge->entryvsize; i++) {
subcfg = ifn->scfge->entryv[i];
if (subcfg->strvsize < 1) {
assert(subcfg->entryvsize > 0);
warnx("%s: blocks within an interface block "
"must be prefixed with a peer key",
ifn->ifname);
e = 1;
continue;
}
key = subcfg->strv[0];
if (strcasecmp("peer", key) != 0) {
if (subcfg->entryvsize > 0) {
warnx("%s: %s may not have a block "
"associated", ifn->ifname, key);
e = 1;
/* check other keywords first */
}
}
if (strcasecmp("user", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must contain one name "
"or uid",
ifn->ifname, key);
e = 1;
continue;
}
if (resolveuser(&ifn->uid, &ifn->gid,
subcfg->strv[1]) == -1) {
warnx("%s: %s invalid: %s",
ifn->ifname, key, subcfg->strv[1]);
e = 1;
continue;
}
} else if (strcasecmp("group", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must contain one name "
"or gid",
ifn->ifname, key);
e = 1;
continue;
}
if (resolvegroup(&ifn->gid, subcfg->strv[1])
== -1) {
warnx("%s: %s invalid: %s",
ifn->ifname, key, subcfg->strv[1]);
e = 1;
continue;
}
} else if (strcasecmp("ifaddr", key) == 0) {
if (subcfg->strvsize < 2) {
warnx("%s: %s must have at least"
" one argument", ifn->ifname, key);
e = 1;
continue;
}
for (j = 1; j < subcfg->strvsize; j++) {
xaddone((void ***)&ifn->ifaddrs,
&ifn->ifaddrssize, (void **)&ifaddr,
sizeof(*ifaddr));
if (parseipcidr(&ifaddr->addr,
&ifaddr->prefixlen,
subcfg->strv[j]) == -1) {
warnx("%s: %s could not "
"parse interface address: "
"%s", ifn->ifname, key,
subcfg->strv[j]);
e = 1;
continue;
}
}
} else if (strcasecmp("psk", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must have a value",
ifn->ifname, key);
e = 1;
continue;
}
if (parsekey(ifn->psk, subcfg->strv[1],
strlen(subcfg->strv[1])) == -1) {
warnx("%s: %s could not parse the "
"interface pre-sharedkey",
ifn->ifname, key);
e = 1;
continue;
}
} else if (strcasecmp("pskfile", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s path missing",
ifn->ifname, key);
e = 1;
continue;
}
if ((ifn->pskfile = strdup(subcfg->strv[1]))
== NULL)
err(1, "parseconfig strdup error ifn "
"pskfile");
} else if (strcasecmp("privkey", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must have a value",
ifn->ifname, key);
e = 1;
continue;
}
if (parsekey(ifn->privkey, subcfg->strv[1],
strlen(subcfg->strv[1])) == -1) {
warnx("%s: %s could not parse the "
"interface private key",
ifn->ifname, key);
e = 1;
continue;
}
} else if (strcasecmp("privkeyfile", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s path missing",
ifn->ifname, key);
e = 1;
continue;
}
if ((ifn->privkeyfile = strdup(subcfg->strv[1]))
== NULL)
err(1, "parseconfig strdup error ifn "
"privkeyfile");
} else if (strcasecmp("desc", key) == 0) {
if (subcfg->strvsize != 2) {
warnx("%s: %s must have a value",
ifn->ifname, key);
e = 1;
continue;
}
if ((ifn->ifdesc = strdup(subcfg->strv[1]))
== NULL)
err(1, "parseconfig strdup error "
"ifdesc");
} else if (strcasecmp("listen", key) == 0) {
if (subcfg->strvsize < 2) {
warnx("%s: %s must have at least one "
"address and port, separated by a "
"colon", ifn->ifname, key);
e = 1;
continue;
}
for (j = 1; j < subcfg->strvsize; j++) {
if (parseaddrport(&listenaddr,
subcfg->strv[j]) == -1) {
warnx("%s: %s parse error: %s. Make "
"sure the address and port are "
"separated by a colon", ifn->ifname,
key, subcfg->strv[j]);
e = 1;
continue;
}
if (listenaddr.ss_family == AF_INET6) {
laddrs6 = reallocarray(ifn->laddrs6,
ifn->laddrs6count + 1,
sizeof(struct sockaddr_in6));
if (laddrs6 == NULL) {
warn("%s: %s "
"reallocarray error"
" %s", ifn->ifname,
key, subcfg->strv[j]);
e = 1;
continue;
}
memcpy(&laddrs6[ifn->laddrs6count],
&listenaddr,
sizeof(struct sockaddr_in6));
ifn->laddrs6 = laddrs6;
ifn->laddrs6count++;
} else if (listenaddr.ss_family == AF_INET) {
laddrs4 = reallocarray(ifn->laddrs4,
ifn->laddrs4count + 1,
sizeof(struct sockaddr_in));
if (laddrs4 == NULL) {
warn("%s: %s "
"reallocarray error"
" %s", ifn->ifname,
key, subcfg->strv[j]);
e = 1;
continue;
}
memcpy(&laddrs4[ifn->laddrs4count],
&listenaddr,
sizeof(struct sockaddr_in));
ifn->laddrs4 = laddrs4;
ifn->laddrs4count++;
} else {
warn("%s: %s "
"specify an IPv6 or IPv4 "
"address %s", ifn->ifname,
key, subcfg->strv[j]);
e = 1;
continue;
}
}
} else if (strcasecmp("peer", key) == 0) {
/*
* Skip for now, first process all interface
* keywords.
*/
} else {
warnx("%s: %s invalid keyword in interface "
"scope", ifn->ifname, key);
e = 1;
}
}
/*
* Ensure defaults.
*/
if (memcmp(ifn->psk, nullkey, sizeof(wskey)) == 0) {
if (ifn->pskfile != NULL) {
rc = parsekeyfile(ifn->psk, ifn->pskfile);