-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.c
574 lines (504 loc) · 17.8 KB
/
parser.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
/*
parser.c - Parsing routines for tnat64.conf
*/
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <config.h>
#include "common.h"
#include "parser.h"
/* Global configuration variables */
#define MAXLINE BUFSIZ /* Max length of conf line */
static struct prefixent *currentcontext = NULL;
static int handle_line(struct parsedfile *, char *, int);
static int tokenize(char *, int, char *[]);
static int handle_path(struct parsedfile *, int, int, char *[]);
static int handle_endpath(struct parsedfile *, int, int, char *[]);
static int handle_subnet(struct parsedfile *, int, char *);
static int handle_local(struct parsedfile *, int, char *);
static int handle_prefix(struct parsedfile *, int, char *);
static int make_netent(char *value, struct netent **ent);
int HIDDENSYM read_config(char *filename, struct parsedfile *config)
{
FILE *conf;
char line[MAXLINE];
int rc = 0;
int lineno = 1;
/* Clear out the structure */
memset(config, 0x0, sizeof(*config));
/* Initialization */
currentcontext = &(config->defaultprefix);
/* If a filename wasn't provided, use the default */
if (filename == NULL)
{
strncpy(line, CONF_FILE, sizeof(line) - 1);
/* Insure null termination */
line[sizeof(line) - 1] = (char)0;
filename = line;
}
show_msg(MSGDEBUG, "Opening configuration file (%s)\n", filename);
/* Read the configuration file */
if ((conf = fopen(filename, "r")) == NULL)
{
show_msg(MSGERR, "Could not open NAT64 configuration file " "(%s), assuming all networks local\n", filename);
handle_local(config, 0, "0.0.0.0/0.0.0.0");
rc = 1; /* Severe errors reading configuration */
}
else
{
memset(&(config->defaultprefix), 0x0, sizeof(config->defaultprefix));
while (NULL != fgets(line, MAXLINE, conf))
{
/* This line _SHOULD_ end in \n so we */
/* just chop off the \n and hand it on */
if (strlen(line) > 0)
line[strlen(line) - 1] = '\0';
handle_line(config, line, lineno);
lineno++;
}
fclose(conf);
/* Always add the 127.0.0.1/255.0.0.0 subnet to local */
handle_local(config, 0, "127.0.0.0/255.0.0.0");
}
return (rc);
}
static int handle_line(struct parsedfile *config, char *line, int lineno)
{
char *words[10];
static char savedline[MAXLINE];
int nowords = 0, i;
/* Save the input string */
strncpy(savedline, line, MAXLINE - 1);
savedline[MAXLINE - 1] = (char)0;
/* Tokenize the input string */
nowords = tokenize(line, 10, words);
/* Set the spare slots to an empty string to simplify */
/* processing */
for (i = nowords; i < 10; i++)
words[i] = "";
if (nowords > 0)
{
/* Now this can either be a "path" block starter or */
/* ender, otherwise it has to be a pair (<name> = */
/* <value>) */
if (!strcmp(words[0], "path"))
{
handle_path(config, lineno, nowords, words);
}
else if (!strcmp(words[0], "}"))
{
handle_endpath(config, lineno, nowords, words);
}
else
{
/* Has to be a pair */
if ((nowords != 3) || (strcmp(words[1], "=")))
{
show_msg(MSGERR, "Malformed configuration pair " "on line %d in configuration " "file, \"%s\"\n", lineno, savedline);
}
else if (!strcmp(words[0], "subnet"))
{
handle_subnet(config, lineno, words[2]);
}
else if (!strcmp(words[0], "nat64_prefix"))
{
handle_prefix(config, lineno, words[2]);
}
else if (!strcmp(words[0], "local"))
{
handle_local(config, lineno, words[2]);
}
else
{
show_msg(MSGERR, "Invalid pair type (%s) specified " "on line %d in configuration file, " "\"%s\"\n", words[0], lineno, savedline);
}
}
}
return (0);
}
/* This routines breaks up input lines into tokens */
/* and places these tokens into the array specified */
/* by tokens */
static int tokenize(char *line, int arrsize, char *tokens[])
{
int tokenno = -1;
int finished = 0;
/* Whitespace is ignored before and after tokens */
while ((tokenno < (arrsize - 1)) && (line = line + strspn(line, " \t")) && (*line != (char)0) && (!finished))
{
tokenno++;
tokens[tokenno] = line;
line = line + strcspn(line, " \t");
*line = (char)0;
line++;
/* We ignore everything after a # */
if (*tokens[tokenno] == '#')
{
finished = 1;
tokenno--;
}
}
return (tokenno + 1);
}
static int handle_path(struct parsedfile *config, int lineno, int nowords, char *words[])
{
struct prefixent *newprefix;
if ((nowords != 2) || (strcmp(words[1], "{")))
{
show_msg(MSGERR, "Badly formed path open statement on line %d " "in configuration file (should look like " "\"path {\")\n", lineno);
}
else if (currentcontext != &(config->defaultprefix))
{
/* You cannot nest path statements so check that */
/* the current context is defaultprefix */
show_msg(MSGERR, "Path statements cannot be nested on line %d " "in configuration file\n", lineno);
}
else
{
/* Open up a new prefixent, put it on the list */
/* then set the current context */
if ((newprefix = (struct prefixent *)malloc(sizeof(struct prefixent))) == NULL)
exit(-1);
/* Initialize the structure */
show_msg(MSGDEBUG, "New prefix structure from line %d in configuration file going " "to 0x%08x\n", lineno, newprefix);
memset(newprefix, 0x0, sizeof(*newprefix));
newprefix->next = config->paths;
newprefix->lineno = lineno;
config->paths = newprefix;
currentcontext = newprefix;
}
return (0);
}
static int handle_endpath(struct parsedfile *config, int lineno, int nowords, char *words[])
{
if (nowords != 1)
{
show_msg(MSGERR, "Badly formed path close statement on line " "%d in configuration file (should look like " "\"}\")\n", lineno);
}
else
{
currentcontext = &(config->defaultprefix);
}
/* We could perform some checking on the validty of data in */
/* the completed path here, but thats what verifyconf is */
/* designed to do, no point in weighing down libtsocks */
return (0);
}
static int handle_subnet(struct parsedfile *config, int lineno, char *value)
{
int rc;
struct netent *ent;
rc = make_netent(value, &ent);
switch (rc)
{
case 1:
show_msg(MSGERR, "Local network specification (%s) is not validly " "constructed in subnet statement on line " "%d in configuration " "file\n", value, lineno);
return (0);
break;
case 2:
show_msg(MSGERR, "IP in subnet statement " "network specification (%s) is not valid on line " "%d in configuration file\n", value, lineno);
return (0);
break;
case 3:
show_msg(MSGERR, "SUBNET in subnet statement " "network specification (%s) is not valid on " "line %d in configuration file\n", value, lineno);
return (0);
break;
case 4:
show_msg(MSGERR, "IP (%s) & ", inet_ntoa(ent->localip));
show_msg(MSGERR, "SUBNET (%s) != IP on line %d in " "configuration file, ignored\n", inet_ntoa(ent->localnet), lineno);
return (0);
break;
case 5:
show_msg(MSGERR, "Start port in subnet statement " "network specification (%s) is not valid on line " "%d in configuration file\n", value, lineno);
return (0);
break;
case 6:
show_msg(MSGERR, "End port in subnet statement " "network specification (%s) is not valid on line " "%d in configuration file\n", value, lineno);
return (0);
break;
case 7:
show_msg(MSGERR, "End port in subnet statement " "network specification (%s) is less than the start " "port on line %d in configuration file\n", value, lineno);
return (0);
break;
}
/* The entry is valid so add it to linked list */
ent->next = currentcontext->reachnets;
currentcontext->reachnets = ent;
return (0);
}
static int handle_prefix(struct parsedfile *config, int lineno, char *value)
{
char *ip;
ip = strsplit(NULL, &value, " ");
if (currentcontext->address == NULL)
{
currentcontext->address = strdup(ip);
if (!inet_pton(AF_INET6, ip, ¤tcontext->prefix))
{
show_msg(MSGERR, "Cannot parse NAT64 prefix " "specified at line %d in " "configuration file\n", lineno);
}
}
else
{
if (currentcontext == &(config->defaultprefix))
show_msg(MSGERR, "Only one default NAT64 prefix " "may be specified at line %d in " "configuration file\n", lineno);
else
show_msg(MSGERR, "Only one NAT64 prefix may be specified " "per path on line %d in configuration " "file. (Path begins on line %d)\n", lineno, currentcontext->lineno);
}
return (0);
}
static int handle_local(struct parsedfile *config, int lineno, char *value)
{
int rc;
struct netent *ent;
if (currentcontext != &(config->defaultprefix))
{
show_msg(MSGERR, "Local networks cannot be specified in path " "block at like %d in configuration file. " "(Path block started at line %d)\n", lineno, currentcontext->lineno);
return (0);
}
rc = make_netent(value, &ent);
switch (rc)
{
case 1:
show_msg(MSGERR, "Local network specification (%s) is not validly " "constructed on line %d in configuration " "file\n", value, lineno);
return (0);
break;
case 2:
show_msg(MSGERR, "IP for local " "network specification (%s) is not valid on line " "%d in configuration file\n", value, lineno);
return (0);
break;
case 3:
show_msg(MSGERR, "SUBNET for " "local network specification (%s) is not valid on " "line %d in configuration file\n", value, lineno);
return (0);
break;
case 4:
show_msg(MSGERR, "IP (%s) & ", inet_ntoa(ent->localip));
show_msg(MSGERR, "SUBNET (%s) != IP on line %d in " "configuration file, ignored\n", inet_ntoa(ent->localnet), lineno);
return (0);
case 5:
case 6:
case 7:
show_msg(MSGERR, "Port specification is invalid and " "not allowed in local network specification " "(%s) on line %d in configuration file\n", value, lineno);
return (0);
break;
}
if (ent->startport || ent->endport)
{
show_msg(MSGERR, "Port specification is " "not allowed in local network specification " "(%s) on line %d in configuration file\n", value, lineno);
return (0);
}
/* The entry is valid so add it to linked list */
ent->next = config->localnets;
(config->localnets) = ent;
return (0);
}
/* Construct a netent given a string like */
/* "198.126.0.1[:portno[-portno]]/255.255.255.0" */
int HIDDENSYM make_netent(char *value, struct netent **ent)
{
char *ip;
char *subnet;
char *startport = NULL;
char *endport = NULL;
char *badchar;
char separator;
static char buf[200];
char *split;
/* Get a copy of the string so we can modify it */
strncpy(buf, value, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = (char)0;
split = buf;
/* Now rip it up */
ip = strsplit(&separator, &split, "/:");
if (separator == ':')
{
/* We have a start port */
startport = strsplit(&separator, &split, "-/");
if (separator == '-')
/* We have an end port */
endport = strsplit(&separator, &split, "/");
}
subnet = strsplit(NULL, &split, " \n");
if ((ip == NULL) || (subnet == NULL))
{
/* Network specification not validly constructed */
return (1);
}
/* Allocate the new entry */
if ((*ent = (struct netent *)malloc(sizeof(struct netent))) == NULL)
{
/* If we couldn't malloc some storage, leave */
exit(1);
}
show_msg(MSGDEBUG, "New network entry for %s\n", ip);
if (!startport)
(*ent)->startport = 0;
if (!endport)
(*ent)->endport = 0;
#ifdef HAVE_INET_ADDR
if (((*ent)->localip.s_addr = inet_addr(ip)) == -1)
{
#elif defined(HAVE_INET_ATON)
if (!(inet_aton(ip, &((*ent)->localip))))
{
#endif
/* Badly constructed IP */
free(*ent);
return (2);
}
#ifdef HAVE_INET_ADDR
else if (((*ent)->localnet.s_addr = inet_addr(subnet)) == -1)
{
#elif defined(HAVE_INET_ATON)
else if (!(inet_aton(subnet, &((*ent)->localnet))))
{
#endif
/* Badly constructed subnet */
free(*ent);
return (3);
}
else if (((*ent)->localip.s_addr & (*ent)->localnet.s_addr) != (*ent)->localip.s_addr)
{
/* Subnet and Ip != Ip */
free(*ent);
return (4);
}
else if (startport && (!((*ent)->startport = strtol(startport, &badchar, 10)) || (*badchar != 0) || ((*ent)->startport > 65535)))
{
/* Bad start port */
free(*ent);
return (5);
}
else if (endport && (!((*ent)->endport = strtol(endport, &badchar, 10)) || (*badchar != 0) || ((*ent)->endport > 65535)))
{
/* Bad end port */
free(*ent);
return (6);
}
else if (((*ent)->startport > (*ent)->endport) && !(startport && !endport))
{
/* End port is less than start port */
free(*ent);
return (7);
}
if (startport && !endport)
(*ent)->endport = (*ent)->startport;
return (0);
}
int HIDDENSYM is_local(struct parsedfile *config, struct in_addr *testip)
{
struct netent *ent;
for (ent = (config->localnets); ent != NULL; ent = ent->next)
{
if ((testip->s_addr & ent->localnet.s_addr) == (ent->localip.s_addr & ent->localnet.s_addr))
{
return (0);
}
}
return (1);
}
/* Find the appropriate prefix to reach an ip */
int HIDDENSYM pick_prefix(struct parsedfile *config, struct prefixent **ent, struct in_addr *ip, uint16_t port)
{
struct netent *net;
char ipbuf[64];
show_msg(MSGDEBUG, "Picking appropriate prefix for %s\n", inet_ntoa(*ip));
*ent = (config->paths);
while (*ent != NULL)
{
/* Go through all the prefixes looking for one */
/* with a path to this network */
show_msg(MSGDEBUG, "Checking NAT64 prefix %s\n", ((*ent)->address ? (*ent)->address : "(No Address)"));
net = (*ent)->reachnets;
while (net != NULL)
{
strcpy(ipbuf, inet_ntoa(net->localip));
show_msg(MSGDEBUG, "%s/%s is reachable through this prefix\n", ipbuf, inet_ntoa(net->localnet));
if (((ip->s_addr & net->localnet.s_addr) == (net->localip.s_addr & net->localnet.s_addr)) && (!net->startport || ((net->startport <= port) && (net->endport >= port))))
{
show_msg(MSGDEBUG, "The target is reachable\n");
/* Found the net, return */
return (0);
}
net = net->next;
}
(*ent) = (*ent)->next;
}
*ent = &(config->defaultprefix);
return (0);
}
int HIDDENSYM check_prefix(struct parsedfile *config, struct in6_addr * addr)
{
struct prefixent *ent;
char addrbuffer[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, addr, addrbuffer, sizeof(addrbuffer)))
{
show_msg(MSGDEBUG, "Checking if IPv6 address %s is behind the NAT64...\n", addrbuffer);
}
ent = (config->paths);
while (ent != NULL)
{
/* Go through all the prefixes */
show_msg(MSGDEBUG, "Checking NAT64 prefix %s\n", (ent->address ? ent->address : "(No Address)"));
if ((ent->address))
{
if (!memcmp(addr, &(ent->prefix), NAT64PREFIXLEN))
{
show_msg(MSGDEBUG, "Match!\n");
return 1;
}
}
ent = ent->next;
}
ent = &(config->defaultprefix);
show_msg(MSGDEBUG, "Checking the default NAT64 prefix %s\n", (ent->address ? ent->address : "(No Address)"));
if (!memcmp(addr, &(ent->prefix), NAT64PREFIXLEN))
{
show_msg(MSGDEBUG, "Match!\n");
return 1;
}
return 0;
}
/* This function is very much like strsep, it looks in a string for */
/* a character from a list of characters, when it finds one it */
/* replaces it with a \0 and returns the start of the string */
/* (basically spitting out tokens with arbitrary separators). If no */
/* match is found the remainder of the string is returned and */
/* the start pointer is set to be NULL. The difference between */
/* standard strsep and this function is that this one will */
/* set *separator to the character separator found if it isn't null */
char HIDDENSYM *strsplit(char *separator, char **text, const char *search)
{
int len;
char *ret;
ret = *text;
if (*text == NULL)
{
if (separator)
*separator = '\0';
return (NULL);
}
else
{
len = strcspn(*text, search);
if (len == strlen(*text))
{
if (separator)
*separator = '\0';
*text = NULL;
}
else
{
*text = *text + len;
if (separator)
*separator = **text;
**text = '\0';
*text = *text + 1;
}
}
return (ret);
}