-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
622 lines (546 loc) · 19.3 KB
/
config.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
//
// Copyright (c) 2024, Denny Page
// 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
// Limits for internal configuration arrays
#define MAX_INPUT_LINE 16384
#define MAX_LIST_ARRAY 1024
// Global section
#define GLOBAL_SECTION "[global]"
// Keys specific to the global section
#define KEY_INTERFACES "interfaces"
#define KEY_DISABLE_PACKET_FILTERING "disable-packet-filtering"
// Keys common to global and interface sections
#define KEY_DISABLE_IPV4 "disable-ipv4"
#define KEY_DISABLE_IPV6 "disable-ipv6"
// Keys specific tointerface sections
#define KEY_ALLOW_INBOUND_FILTERS "allow-inbound-filters"
#define KEY_DENY_INBOUND_FILTERS "deny-inbound-filters"
#define KEY_ALLOW_OUTBOUND_FILTERS "allow-outbound-filters"
#define KEY_DENY_OUTBOUND_FILTERS "deny-outbound-filters"
// Global config flags
static unsigned int global_disable_ipv4 = 0;
static unsigned int global_disable_ipv6 = 0;
// Current configuration line
static unsigned int config_lineno = 0;
//
// Trim (skip) leading whitespace from a string
//
static char * trim_leading_whitespace(
char * str)
{
while (isspace(*str))
{
str += 1;
}
return (str);
}
//
// Trim trailing whitespace from a string
//
static void trim_trailing_whitespace(
char * str)
{
char * end;
end = str + strlen(str) - 1;
while (isspace(*end))
{
*end = 0;
end -= 1;
}
}
//
// Split a line into a key and value
//
static char * split_keyvalue(
char * line)
{
char * value;
// Ensure it's actually an assignment
value = strchr(line, '=');
if (value == NULL)
{
fatal("%s line %d: Syntax error - missing assignment\n", config_filename, config_lineno);
}
*value = 0;
// Trim the value and ensure it is not empty
value = trim_leading_whitespace(value + 1);
if (*value == 0)
{
fatal("%s line %d: Syntax error - missing value\n", config_filename, config_lineno);
}
// Trim the key
trim_trailing_whitespace(line);
return (value);
}
//
// Convert a comma separated list of strings into a sorted array
// NB: The array MUST be at least MAX_LIST_ARRAY in size
//
static unsigned int split_comma_list(
char * str,
char ** array)
{
unsigned int index = 0;
// Add the first element to the array
array[0] = str;
while (*str)
{
if (*str == ',')
{
if (index + 1 >= MAX_LIST_ARRAY)
{
fatal("%s line %d: Invalid list - elements exceed max allowed (%u)\n", config_filename, config_lineno, MAX_LIST_ARRAY);
}
// Terminate the current element
*str = 0;
trim_trailing_whitespace(str);
// Ensure the current element is not empty
if (array[index] == str)
{
fatal("%s line %d: Invalid list - empty element\n", config_filename, config_lineno);
}
// Insure the next element is not empty
str = trim_leading_whitespace(str + 1);
if (*str == 0)
{
fatal("%s line %d: Invalid list - empty element\n", config_filename, config_lineno);
}
// Add the new element to the array
index += 1;
array[index] = str;
}
else
{
str += 1;
}
}
return (index + 1);
}
//
// Read a line from the config file
// NB: The buffer MUST be at least MAX_INPUT_LINE in size
//
static char * read_line(
FILE * fp,
char * buffer)
{
char * line;
while (fgets(buffer, MAX_INPUT_LINE, fp) != NULL)
{
config_lineno += 1;
// Trim leading whitespace spaces
line = trim_leading_whitespace(buffer);
// Ignore empty and comment lines
if (*line == 0 || *line == '#')
{
continue;
}
// Trim trailing whitespace
trim_trailing_whitespace(line);
return line;
}
return NULL;
}
//
// Read and process the config file
//
void read_config(void)
{
FILE * fp;
char buffer[MAX_INPUT_LINE];
char * list_array[MAX_LIST_ARRAY];
unsigned int list_array_count;
interface_t * interface;
char * line;
char * value;
unsigned int offset;
unsigned int index;
// Open the config file
fp = fopen(config_filename, "r");
if (fp == NULL)
{
fatal("Unable to open config file \"%s\"\n", config_filename);
}
// Ensure the global section is the first section in the config file
line = read_line(fp, buffer);
if ((line == NULL) || strcmp(line, GLOBAL_SECTION) != 0)
{
fatal("%s: File does not contain [global] as the first section\n", config_filename);
}
// Process the lines in the global section
while ((line = read_line(fp, buffer)))
{
if (*line == '[')
{
break;
}
// Split the key/value pair
value = split_keyvalue(line);
if (strcmp(line, KEY_INTERFACES) == 0)
{
list_array_count = split_comma_list(value, list_array);
if (list_array_count < 2)
{
fatal("%s line %d: A minimum of 2 interfaces are required\n", config_filename, config_lineno);
}
if (set_interface_list(list_array, list_array_count))
{
fatal("%s line %d: Only one interface list is allowed\n", config_filename, config_lineno);
}
}
else if (strcmp(line, KEY_DISABLE_IPV4) == 0)
{
if (strcmp(value, "yes") == 0)
{
global_disable_ipv4 = 1;
}
else if (strcmp(value, "no") == 0)
{
global_disable_ipv4 = 0;
}
else
{
fatal("%s line %d: Invalid value for %s \"%s\"\n", config_filename, config_lineno, KEY_DISABLE_IPV4, value);
}
}
else if (strcmp(line, KEY_DISABLE_IPV6) == 0)
{
if (strcmp(value, "yes") == 0)
{
global_disable_ipv6 = 1;
}
else if (strcmp(value, "no") == 0)
{
global_disable_ipv6 = 0;
}
else
{
fatal("%s line %d: Invalid value for %s \"%s\"\n", config_filename, config_lineno, KEY_DISABLE_IPV6, value);
}
}
else if (strcmp(line, KEY_DISABLE_PACKET_FILTERING) == 0)
{
if (strcmp(value, "yes") == 0)
{
if (global_filter_list)
{
fatal("%s line %d: %s cannot be combined with %s or %s\n", config_filename, config_lineno,
KEY_DISABLE_PACKET_FILTERING, KEY_ALLOW_INBOUND_FILTERS, KEY_DENY_INBOUND_FILTERS);
}
filtering_enabled = 0;
}
else if (strcmp(value, "no") == 0)
{
filtering_enabled = 1;
}
else
{
fatal("%s line %d: Invalid value for %s \"%s\"\n", config_filename, config_lineno,
KEY_DISABLE_PACKET_FILTERING, value);
}
}
else if (strcmp(line, KEY_ALLOW_INBOUND_FILTERS) == 0)
{
if (filtering_enabled == 0)
{
fatal("%s line %d: %s cannot be combined with %s\n", config_filename, config_lineno,
KEY_ALLOW_INBOUND_FILTERS, KEY_DISABLE_PACKET_FILTERING);
}
list_array_count = split_comma_list(value, list_array);
if (set_global_filter_list(ALLOW, list_array, list_array_count))
{
fatal("%s line %d: Only one global filter list is allowed\n", config_filename, config_lineno);
}
}
else if (strcmp(line, KEY_DENY_INBOUND_FILTERS) == 0)
{
if (filtering_enabled == 0)
{
fatal("%s line %d: %s cannot be combined with %s\n", config_filename, config_lineno,
KEY_DENY_INBOUND_FILTERS, KEY_DISABLE_PACKET_FILTERING);
}
list_array_count = split_comma_list(value, list_array);
if (set_global_filter_list(DENY, list_array, list_array_count))
{
fatal("%s line %d: Only one global filter list is allowed\n", config_filename, config_lineno);
}
}
else
{
fatal("%s line %d: Unknown [global] parameter \"%s\"\n", config_filename, config_lineno, line);
}
}
// Ensure we found an interface list
if (configured_interface_list == NULL)
{
fatal("%s: [global] section missing required parameter \"%s\"\n", config_filename, KEY_INTERFACES);
}
// Initialize the interface IP settings to match global settings
if (global_disable_ipv4)
{
for (index = 0; index < configured_interface_count; index++)
{
configured_interface_list[index].disable_ip[IPV4] = 1;
}
}
if (global_disable_ipv6)
{
for (index = 0; index < configured_interface_count; index++)
{
configured_interface_list[index].disable_ip[IPV6] = 1;
}
}
// Process lines in interface sections
while (line && line[0] == '[')
{
// Ignore leading whitespace
line = trim_leading_whitespace(line + 1);
// Ensure the section name (interface name)is terminated
offset = strlen(line) - 1;
if (line[offset] != ']')
{
fatal("%s line %d: Syntax error\n", config_filename, config_lineno);
}
line[offset] = 0;
// Ignore trailing whitespace
trim_trailing_whitespace(line);
// Insure the interface name is valid
if (strlen(line) == 0 || strpbrk(line, "[]") != NULL)
{
fatal("%s line %d: Syntax error\n", config_filename, config_lineno);
}
// Find the interface
interface = get_interface_by_name(line);
if (interface == NULL)
{
fatal("%s line %d: Interface \"%s\" is not in the [global] interfaces list\n", config_filename, config_lineno, line);
}
// Read the rest of the interface section
while ((line = read_line(fp, buffer)))
{
if (*line == '[')
{
break;
}
// Split the key/value pair
value = split_keyvalue(line);
if (strcmp(line, KEY_DISABLE_IPV4) == 0)
{
if (strcmp(value, "yes") == 0)
{
interface->disable_ip[IPV4] = 1;
}
else if (strcmp(value, "no") == 0)
{
if (global_disable_ipv4)
{
fatal("%s line %d: IPv4 is globally disabled\n", config_filename, config_lineno);
}
interface->disable_ip[IPV4] = 0;
}
else
{
fatal("%s line %d: Invalid value for %s \"%s\"\n", config_filename, config_lineno, KEY_DISABLE_IPV4, value);
}
}
else if (strcmp(line, KEY_DISABLE_IPV6) == 0)
{
if (strcmp(value, "yes") == 0)
{
interface->disable_ip[IPV6] = 1;
}
else if (strcmp(value, "no") == 0)
{
if (global_disable_ipv6)
{
fatal("%s line %d: IPv6 is globally disabled\n", config_filename, config_lineno);
}
interface->disable_ip[IPV6] = 0;
}
else
{
fatal("%s line %d: Invalid value for %s \"%s\"\n", config_filename, config_lineno, KEY_DISABLE_IPV6, value);
}
}
else if (strcmp(line, KEY_ALLOW_INBOUND_FILTERS) == 0)
{
if (filtering_enabled == 0)
{
fatal("%s line %d: %s cannot be combined with %s\n", config_filename, config_lineno,
KEY_ALLOW_INBOUND_FILTERS, KEY_DISABLE_PACKET_FILTERING);
}
list_array_count = split_comma_list(value, list_array);
if (set_interface_inbound_filter_list(interface, ALLOW, list_array, list_array_count))
{
fatal("%s line %d: Only one inbound filter list per interface is allowed\n", config_filename, config_lineno);
}
}
else if (strcmp(line, KEY_DENY_INBOUND_FILTERS) == 0)
{
if (filtering_enabled == 0)
{
fatal("%s line %d: %s cannot be combined with %s\n", config_filename, config_lineno,
KEY_DENY_INBOUND_FILTERS, KEY_DISABLE_PACKET_FILTERING);
}
list_array_count = split_comma_list(value, list_array);
if (set_interface_inbound_filter_list(interface, DENY, list_array, list_array_count))
{
fatal("%s line %d: Only one inbound filter list per interface is allowed\n", config_filename, config_lineno);
}
}
else if (strcmp(line, KEY_ALLOW_OUTBOUND_FILTERS) == 0)
{
if (filtering_enabled == 0)
{
fatal("%s line %d: %s cannot be combined with %s\n", config_filename, config_lineno,
KEY_ALLOW_OUTBOUND_FILTERS, KEY_DISABLE_PACKET_FILTERING);
}
list_array_count = split_comma_list(value, list_array);
if (set_interface_outbound_filter_list(interface, ALLOW, list_array, list_array_count))
{
fatal("%s line %d: Only one outbound filter list per interface is allowed\n", config_filename, config_lineno);
}
}
else if (strcmp(line, KEY_DENY_OUTBOUND_FILTERS) == 0)
{
if (filtering_enabled == 0)
{
fatal("%s line %d: %s cannot be combined with %s\n", config_filename, config_lineno,
KEY_DENY_OUTBOUND_FILTERS, KEY_DISABLE_PACKET_FILTERING);
}
list_array_count = split_comma_list(value, list_array);
if (set_interface_outbound_filter_list(interface, DENY, list_array, list_array_count))
{
fatal("%s line %d: Only one outbound filter list per interface is allowed\n", config_filename, config_lineno);
}
}
else
{
fatal("%s line %d: Unknown interface parameter \"%s\"\n", config_filename, config_lineno, line);
}
}
}
if (line != NULL)
{
fatal("%s line %d: Syntax error\n", config_filename, config_lineno);
}
fclose(fp);
}
//
// Dump the configuration
//
static void dump_filter_list(
char * name,
filter_list_t * list)
{
unsigned int index;
unsigned char string[DNS_MAX_NAME_LEN];
if (list)
{
printf(" %s (%s):\n", name, list->allow_deny == ALLOW ? "allow" : "deny");
for (index = 0; index < list->count; index++)
{
dns_labels_to_string(list->names[index]->labels, list->names[index]->length, string);
printf(" %s\n", string);
}
}
else
{
printf(" %s: (none)\n", name);
}
}
//
// Dump the configuration
//
void dump_config(void)
{
interface_t * interface;
unsigned int index;
unsigned int peer;
// Global section
printf("\nGlobal settings:\n");
if (global_disable_ipv4) {
printf(" disable ipv4 = true\n");
} else {
printf(" disable ipv4 = false\n");
}
if (global_disable_ipv6) {
printf(" disable ipv6 = true\n");
} else {
printf(" disable ipv6 = false\n");
}
dump_filter_list("global filter", global_filter_list);
// Interfaces
printf("\nInterface list:\n");
for (index = 0; index < configured_interface_count; index++)
{
interface = &configured_interface_list[index];
printf(" %s (%u)\n", interface->name, interface->if_index);
if (interface->disable_ip[IPV4] == 0)
{
printf(" ipv4 address %s\n", interface->ipv4_addr_str);
printf(" peer interfaces:");
for (peer = 0; peer < interface->peer_count[IPV4]; peer++)
{
printf(" %s", interface->peer_list[IPV4][peer]->name);
}
printf("\n");
}
else
{
printf(" ipv4 disabled\n");
}
if (interface->disable_ip[IPV6] == 0)
{
printf(" ipv6 address %s\n", interface->ipv6_addr_str);
printf(" peer interfaces:");
for (peer = 0; peer < interface->peer_count[IPV6]; peer++)
{
printf(" %s", interface->peer_list[IPV6][peer]->name);
}
printf("\n");
}
else
{
printf(" ipv6 disabled\n");
}
if (interface->inbound_filter_list)
{
dump_filter_list("inbound filter list", interface->inbound_filter_list);
}
if (interface->outbound_filter_list)
{
dump_filter_list("outbound filter list", interface->outbound_filter_list);
}
printf("\n");
}
}