-
Notifications
You must be signed in to change notification settings - Fork 2
/
conf.c
754 lines (621 loc) · 17.4 KB
/
conf.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
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2015 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ini_configobj.h>
#include <string.h>
#include <strings.h>
#include <sys/utsname.h>
#include "conf.h"
#include "util.h"
struct local_conf {
struct conf *conf;
int default_port;
};
static int compare_connect_entries(const struct iv_avl_node *_a,
const struct iv_avl_node *_b)
{
const struct conf_connect_entry *a;
const struct conf_connect_entry *b;
a = iv_container_of(_a, struct conf_connect_entry, an);
b = iv_container_of(_b, struct conf_connect_entry, an);
return strcmp(a->name, b->name);
}
static int compare_listening_sockets(const struct iv_avl_node *_a,
const struct iv_avl_node *_b)
{
const struct conf_listening_socket *a;
const struct conf_listening_socket *b;
a = iv_container_of(_a, struct conf_listening_socket, an);
b = iv_container_of(_b, struct conf_listening_socket, an);
return addrcmp((struct sockaddr *)&a->listen_address,
(struct sockaddr *)&b->listen_address);
}
static struct ini_cfgobj *parse_cfgfile(const char *file)
{
struct ini_cfgobj *co;
struct ini_cfgfile *cf;
if (ini_config_create(&co)) {
fprintf(stderr, "error creating ini config object\n");
return NULL;
}
if (ini_config_file_open(file, 0, &cf)) {
fprintf(stderr, "error opening %s\n", file);
ini_config_destroy(co);
return NULL;
}
if (ini_config_parse(cf, INI_STOP_ON_ANY,
INI_MS_ERROR | INI_MV1S_ALLOW, 0, co)) {
fprintf(stderr, "error parsing configuration file\n");
if (ini_config_error_count(co)) {
char **errors = NULL;
ini_config_get_errors(co, &errors);
if (errors != NULL) {
ini_config_print_errors(stderr, errors);
ini_config_free_errors(errors);
}
}
ini_config_file_destroy(cf);
ini_config_destroy(co);
return NULL;
}
ini_config_file_destroy(cf);
return co;
}
static int parse_config_default(struct local_conf *lc, struct ini_cfgobj *co)
{
struct value_obj *vo;
int ret;
ret = ini_get_config_valueobj("default", "NodeName", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
char *key;
key = ini_get_string_config_value(vo, &ret);
if (ret) {
fprintf(stderr, "error retrieving NodeName value\n");
return -1;
}
lc->conf->node_name = key;
} else {
struct utsname buf;
if (uname(&buf) < 0) {
lc->conf->node_name = NULL;
} else {
char *c;
c = strchr(buf.nodename, '.');
if (c != NULL)
*c = 0;
lc->conf->node_name = strdup(buf.nodename);
}
}
ret = ini_get_config_valueobj("default", "PrivateKey", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
char *key;
key = ini_get_string_config_value(vo, &ret);
if (ret) {
fprintf(stderr, "error retrieving PrivateKey value\n");
return -1;
}
lc->conf->private_key = key;
} else {
lc->conf->private_key = strdup("/etc/pki/tls/dvpn/dvpn.key");
}
ret = ini_get_config_valueobj("default", "PublicKey", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
char *key;
key = ini_get_string_config_value(vo, &ret);
if (ret) {
fprintf(stderr, "error retrieving PublicKey value\n");
return -1;
}
lc->conf->public_key = key;
} else {
lc->conf->public_key = strdup("/etc/pki/tls/dvpn/dvpn.pub");
}
ret = ini_get_config_valueobj("default", "RoleKey", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
char *key;
key = ini_get_string_config_value(vo, &ret);
if (ret) {
fprintf(stderr, "error retrieving RoleKey value\n");
return -1;
}
lc->conf->role_key = key;
} else {
lc->conf->role_key = strdup("/etc/pki/tls/dvpn/role.key");
}
ret = ini_get_config_valueobj("default", "DefaultPort", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
int port;
port = ini_get_int_config_value(vo, 1, 0, &ret);
if (ret) {
fprintf(stderr, "error retrieving DefaultPort value\n");
return -1;
}
lc->default_port = port;
}
return 0;
}
static const char *
get_const_value(struct ini_cfgobj *co, const char *section, const char *name)
{
struct value_obj *vo;
int ret;
const char *value;
ret = ini_get_config_valueobj(section, name, co,
INI_GET_FIRST_VALUE, &vo);
if (ret)
return NULL;
value = ini_get_const_string_config_value(vo, &ret);
if (ret)
return NULL;
return value;
}
static int parse_fingerprint(uint8_t *id, const char *fp)
{
if (sscanf(fp, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:"
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:"
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:"
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&id[0], &id[1], &id[2], &id[3],
&id[4], &id[5], &id[6], &id[7],
&id[8], &id[9], &id[10], &id[11],
&id[12], &id[13], &id[14], &id[15],
&id[16], &id[17], &id[18], &id[19],
&id[20], &id[21], &id[22], &id[23],
&id[24], &id[25], &id[26], &id[27],
&id[28], &id[29], &id[30], &id[31]) == NODE_ID_LEN) {
return 0;
}
if (strlen(fp) == 52)
return parse_base32_fingerprint(id, fp);
return -1;
}
static enum conf_peer_type parse_peer_type(const char *pt)
{
if (!strcasecmp(pt, "dbonly"))
return CONF_PEER_TYPE_DBONLY;
if (!strcasecmp(pt, "epeer") || !strcasecmp(pt, "peer"))
return CONF_PEER_TYPE_EPEER;
if (!strcasecmp(pt, "customer") || !strcasecmp(pt, "downstream"))
return CONF_PEER_TYPE_CUSTOMER;
if (!strcasecmp(pt, "transit") || !strcasecmp(pt, "upstream"))
return CONF_PEER_TYPE_TRANSIT;
if (!strcasecmp(pt, "ipeer"))
return CONF_PEER_TYPE_IPEER;
fprintf(stderr, "error parsing peer type '%s'\n", pt);
return CONF_PEER_TYPE_INVALID;
}
static int
add_connect_peer(struct local_conf *lc, const char *peer, const char *connect,
enum conf_fp_type fp_type, const uint8_t *fp,
enum conf_peer_type peer_type, const char *itf, int cost,
int cost_add_rtt)
{
struct conf_connect_entry *cce;
char *delim;
int port;
delim = strstr(connect, "]:");
if (delim != NULL) {
delim++;
} else {
delim = strchr(connect, ':');
if (delim != NULL && strchr(delim + 1, ':') != NULL)
delim = NULL;
}
if (delim != NULL) {
if (sscanf(delim + 1, "%d", &port) != 1) {
fprintf(stderr, "error parsing port number in '%s'\n",
connect);
return -1;
}
} else {
port = lc->default_port;
}
cce = calloc(1, sizeof(*cce));
if (cce == NULL) {
fprintf(stderr, "error allocating memory for cce object\n");
return -1;
}
cce->name = strdup(peer);
if (iv_avl_tree_insert(&lc->conf->connect_entries, &cce->an) < 0) {
fprintf(stderr, "found duplicate connect entry '%s'\n", peer);
free(cce->name);
return -1;
}
if (connect[0] == '[') {
cce->hostname = strdup(connect + 1);
delim = strchr(cce->hostname, ']');
if (delim != NULL)
*delim = 0;
} else {
cce->hostname = strdup(connect);
if (delim != NULL)
cce->hostname[delim - connect] = 0;
}
asprintf(&cce->port, "%d", port);
cce->fp_type = fp_type;
memcpy(cce->fingerprint, fp, NODE_ID_LEN);
cce->peer_type = peer_type;
cce->tunitf = strdup(itf ? : "dvpn%d");
cce->cost = cost;
cce->cost_add_rtt = cost_add_rtt;
return 0;
}
static int parse_listen_addr(struct sockaddr_in6 *dst,
const char *listen, int default_port)
{
char *delim;
int port;
char *l;
struct sockaddr_in6 *a6;
struct sockaddr_in *a4;
int ret;
delim = strstr(listen, "]:");
if (delim != NULL) {
delim++;
} else {
delim = strchr(listen, ':');
if (delim != NULL && strchr(delim + 1, ':') != NULL)
delim = NULL;
}
if (delim != NULL) {
if (sscanf(delim + 1, "%d", &port) != 1) {
fprintf(stderr, "error parsing port number in '%s'\n",
listen);
return -1;
}
} else {
port = default_port;
}
if (listen[0] == '[') {
l = strdup(listen + 1);
delim = strchr(l, ']');
if (delim != NULL)
*delim = 0;
} else {
l = strdup(listen);
if (delim != NULL)
l[delim - listen] = 0;
}
a6 = (struct sockaddr_in6 *)dst;
a6->sin6_family = AF_INET6;
a6->sin6_port = htons(port);
a6->sin6_flowinfo = 0;
a6->sin6_scope_id = 0;
ret = 0;
if (!strcmp(l, "") || !strcmp(l, "*")) {
a6->sin6_addr = in6addr_any;
} else if (!inet_pton(AF_INET6, l, &a6->sin6_addr)) {
a4 = (struct sockaddr_in *)dst;
a4->sin_family = AF_INET;
a4->sin_port = htons(port);
if (!inet_pton(AF_INET, l, &a4->sin_addr)) {
fprintf(stderr, "error parsing address '%s'\n", l);
ret = -1;
}
}
free(l);
return ret;
}
static int compare_listen_entries(const struct iv_avl_node *_a,
const struct iv_avl_node *_b)
{
const struct conf_listen_entry *a;
const struct conf_listen_entry *b;
a = iv_container_of(_a, struct conf_listen_entry, an);
b = iv_container_of(_b, struct conf_listen_entry, an);
return strcmp(a->name, b->name);
}
static struct conf_listening_socket *
get_listening_socket(struct local_conf *lc, const char *listen)
{
struct sockaddr_in6 addr;
struct iv_avl_node *an;
struct conf_listening_socket *cls;
if (parse_listen_addr(&addr, listen, lc->default_port) < 0)
return NULL;
an = lc->conf->listening_sockets.root;
while (an != NULL) {
int ret;
cls = iv_container_of(an, struct conf_listening_socket, an);
ret = addrcmp((struct sockaddr *)&addr,
(struct sockaddr *)&cls->listen_address);
if (ret == 0)
return cls;
if (ret < 0)
an = an->left;
else
an = an->right;
}
cls = calloc(1, sizeof(*cls));
if (cls == NULL) {
fprintf(stderr, "error allocating memory for cls object\n");
return NULL;
}
cls->listen_address = addr;
if (iv_avl_tree_insert(&lc->conf->listening_sockets, &cls->an) < 0)
abort();
cls->have_wildcard_listen_entry = 0;
INIT_IV_AVL_TREE(&cls->listen_entries, compare_listen_entries);
return cls;
}
static int
add_listen_peer(struct local_conf *lc, const char *peer, const char *listen,
enum conf_fp_type fp_type, const uint8_t *fp,
enum conf_peer_type peer_type, const char *itf, int cost,
int cost_add_rtt, int conn_limit)
{
struct conf_listening_socket *cls;
struct conf_listen_entry *cle;
cls = get_listening_socket(lc, listen);
if (cls == NULL)
return -1;
if (cls->have_wildcard_listen_entry && fp_type == CONF_FP_TYPE_ANY) {
fprintf(stderr, "error adding peer object for '%s': "
"already have a PeerFingerprint=any "
"object for this listening address\n", peer);
return -1;
}
cle = calloc(1, sizeof(*cle));
if (cle == NULL) {
fprintf(stderr, "error allocating memory for cle object\n");
return -1;
}
cle->name = strdup(peer);
iv_avl_tree_insert(&cls->listen_entries, &cle->an);
cle->fp_type = fp_type;
memcpy(cle->fingerprint, fp, NODE_ID_LEN);
cle->peer_type = peer_type;
cle->tunitf = strdup(itf ? : "dvpn%d");
cle->cost = cost;
cle->cost_add_rtt = cost_add_rtt;
cle->conn_limit = conn_limit;
if (fp_type == CONF_FP_TYPE_ANY)
cls->have_wildcard_listen_entry = 1;
return 0;
}
static int parse_config_peer(struct local_conf *lc,
struct ini_cfgobj *co, const char *peer)
{
const char *connect;
const char *listen;
int have_cfp;
uint8_t cfp[NODE_ID_LEN];
const char *fp;
uint8_t f[NODE_ID_LEN];
int fp_type;
const char *peertype;
enum conf_peer_type peer_type;
const char *itf;
struct value_obj *vo;
int ret;
int cost;
int cost_add_rtt;
int conn_limit;
connect = get_const_value(co, peer, "Connect");
listen = get_const_value(co, peer, "Listen");
if (!!(connect == NULL) == !!(listen == NULL)) {
fprintf(stderr, "peer object for '%s' needs either a "
"Connect or a Listen directive\n", peer);
return -1;
}
have_cfp = 0;
if (connect != NULL && !parse_hostname_fingerprint(cfp, connect))
have_cfp = 1;
fp = get_const_value(co, peer, "PeerFingerprint");
if (fp == NULL) {
fp_type = -1;
} else if (!strcmp(fp, "any")) {
fp_type = CONF_FP_TYPE_ANY;
} else if (!strcmp(fp, "cname")) {
if (connect == NULL) {
fprintf(stderr, "peer object for '%s' is a listen "
"object yet specifies "
"PeerFingerprint=cname\n", peer);
return -1;
}
fp_type = CONF_FP_TYPE_CNAME;
} else if (parse_fingerprint(f, fp) == 0) {
fp_type = CONF_FP_TYPE_MATCH;
} else {
fprintf(stderr, "peer object for '%s' has an unparseable "
"PeerFingerprint '%s'\n", peer, fp);
return -1;
}
if (!have_cfp && fp_type == -1) {
if (connect != NULL) {
fprintf(stderr, "peer object for '%s' needs either "
"a PeerFingerprint directive or a "
"Connect= hostname with embedded "
"fingerprint\n", peer);
} else {
fprintf(stderr, "peer object for '%s' lacks a "
"PeerFingerprint directive\n", peer);
}
return -1;
}
if (have_cfp && fp_type == CONF_FP_TYPE_MATCH &&
memcmp(cfp, f, NODE_ID_LEN)) {
fprintf(stderr, "peer object for '%s' has a "
"PeerFingerprint= value that conflicts "
"with the fingerprint embedded in the "
"Connect= hostname\n", peer);
return -1;
}
if (have_cfp && fp_type == -1) {
fp_type = CONF_FP_TYPE_MATCH;
memcpy(f, cfp, NODE_ID_LEN);
}
peertype = get_const_value(co, peer, "PeerType");
if (peertype != NULL) {
peer_type = parse_peer_type(peertype);
if (peer_type == CONF_PEER_TYPE_INVALID)
return -1;
} else {
peer_type = (connect != NULL) ?
CONF_PEER_TYPE_TRANSIT :
CONF_PEER_TYPE_CUSTOMER;
}
itf = get_const_value(co, peer, "TunInterface");
ret = ini_get_config_valueobj(peer, "Cost", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
const char *str;
str = ini_get_const_string_config_value(vo, &ret);
if (ret) {
fprintf(stderr, "error retrieving Cost string\n");
return -1;
}
if (str[0] == '+')
cost_add_rtt = 1;
else
cost_add_rtt = 0;
cost = ini_get_int_config_value(vo, 1, 0, &ret);
if (ret) {
fprintf(stderr, "error retrieving Cost value\n");
return -1;
}
if (!cost_add_rtt && (cost < 1 || cost > 32767)) {
fprintf(stderr, "peer Cost must be in [1..32767]\n");
return -1;
} else if (cost_add_rtt && (cost < 0 || cost > 32767)) {
fprintf(stderr, "peer Cost must be in +[0..32767]\n");
return -1;
}
} else {
cost = 0;
cost_add_rtt = 1;
}
ret = ini_get_config_valueobj(peer, "ConnectionLimit", co,
INI_GET_FIRST_VALUE, &vo);
if (ret == 0 && vo != NULL) {
conn_limit = ini_get_int_config_value(vo, 1, 0, &ret);
if (ret) {
fprintf(stderr, "error retrieving ConnectionLimit "
"value\n");
return -1;
}
if (conn_limit < 1) {
fprintf(stderr, "peer ConnectionLimit must be >= 1\n");
return -1;
}
} else {
conn_limit = 1;
}
if (connect != NULL) {
return add_connect_peer(lc, peer, connect, fp_type, f,
peer_type, itf, cost, cost_add_rtt);
} else {
return add_listen_peer(lc, peer, listen, fp_type, f,
peer_type, itf, cost, cost_add_rtt,
conn_limit);
}
return 0;
}
struct conf *parse_config(const char *file)
{
struct conf *conf;
struct ini_cfgobj *co;
struct local_conf lc;
char **section;
int num_sections;
int err;
conf = malloc(sizeof(*conf));
if (conf == NULL) {
fprintf(stderr, "error allocating memory for conf object\n");
return NULL;
}
conf->private_key = NULL;
conf->public_key = NULL;
conf->node_name = NULL;
INIT_IV_AVL_TREE(&conf->connect_entries, compare_connect_entries);
INIT_IV_AVL_TREE(&conf->listening_sockets, compare_listening_sockets);
co = parse_cfgfile(file);
if (co == NULL) {
free(conf);
return NULL;
}
lc.conf = conf;
lc.default_port = 19275;
if (parse_config_default(&lc, co) < 0) {
ini_config_destroy(co);
free(conf);
return NULL;
}
section = ini_get_section_list(co, &num_sections, &err);
if (section != NULL) {
int i;
for (i = 0; i < num_sections; i++) {
if (strcasecmp(section[i], "default") == 0)
continue;
if (parse_config_peer(&lc, co, section[i]) < 0) {
ini_free_section_list(section);
ini_config_destroy(co);
free(conf);
return NULL;
}
}
ini_free_section_list(section);
}
ini_config_destroy(co);
return conf;
}
void free_config(struct conf *conf)
{
struct iv_avl_node *an;
struct iv_avl_node *an2;
free(conf->node_name);
free(conf->private_key);
free(conf->public_key);
free(conf->role_key);
iv_avl_tree_for_each_safe (an, an2, &conf->connect_entries) {
struct conf_connect_entry *cce;
cce = iv_container_of(an, struct conf_connect_entry, an);
iv_avl_tree_delete(&conf->connect_entries, &cce->an);
free(cce->name);
free(cce->hostname);
free(cce->port);
free(cce->tunitf);
free(cce);
}
iv_avl_tree_for_each_safe (an, an2, &conf->listening_sockets) {
struct conf_listening_socket *cls;
struct iv_avl_node *bn;
struct iv_avl_node *bn2;
cls = iv_container_of(an, struct conf_listening_socket, an);
iv_avl_tree_delete(&conf->listening_sockets, &cls->an);
iv_avl_tree_for_each_safe (bn, bn2, &cls->listen_entries) {
struct conf_listen_entry *cle;
cle = iv_container_of(bn, struct conf_listen_entry, an);
iv_avl_tree_delete(&cls->listen_entries, &cle->an);
free(cle->name);
free(cle->tunitf);
free(cle);
}
free(cls);
}
free(conf);
}