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
|
/* $OpenBSD: parse.c,v 1.14 2013/04/17 19:26:10 krw Exp $ */
/* Common parser code for dhcpd and dhclient. */
/*
* Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
* 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.
* 3. Neither the name of The Internet Software Consortium nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM 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 INTERNET SOFTWARE CONSORTIUM 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.
*
* This software has been written for the Internet Software Consortium
* by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
* Enterprises. To learn more about the Internet Software Consortium,
* see ``http://www.vix.com/isc''. To learn more about Vixie
* Enterprises, see ``http://www.vix.com''.
*/
#include "dhcpd.h"
#include "dhctoken.h"
/*
* Skip to the semicolon ending the current statement. If we encounter
* braces, the matching closing brace terminates the statement. If we
* encounter a right brace but haven't encountered a left brace, return
* leaving the brace in the token buffer for the caller. If we see a
* semicolon and haven't seen a left brace, return. This lets us skip
* over:
*
* statement;
* statement foo bar { }
* statement foo bar { statement { } }
* statement}
*
* ...et cetera.
*/
void
skip_to_semi(FILE *cfile)
{
int token;
char *val;
int brace_count = 0;
do {
token = peek_token(&val, cfile);
if (token == '}') {
if (brace_count) {
token = next_token(&val, cfile);
if (!--brace_count)
return;
} else
return;
} else if (token == '{') {
brace_count++;
} else if (token == ';' && !brace_count) {
token = next_token(&val, cfile);
return;
} else if (token == '\n') {
/*
* EOL only happens when parsing
* /etc/resolv.conf, and we treat it like a
* semicolon because the resolv.conf file is
* line-oriented.
*/
token = next_token(&val, cfile);
return;
}
token = next_token(&val, cfile);
} while (token != EOF);
}
int
parse_semi(FILE *cfile)
{
int token;
char *val;
token = next_token(&val, cfile);
if (token != ';') {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
return (0);
}
return (1);
}
/*
* string-parameter :== STRING SEMI
*/
char *
parse_string(FILE *cfile)
{
char *val, *s;
int token;
token = next_token(&val, cfile);
if (token != TOK_STRING) {
parse_warn("filename must be a string");
skip_to_semi(cfile);
return (NULL);
}
s = strdup(val);
if (s == NULL)
error("no memory for string %s.", val);
if (!parse_semi(cfile)) {
free(s);
return (NULL);
}
return (s);
}
/*
* hostname :== identifier | hostname DOT identifier
*/
char *
parse_host_name(FILE *cfile)
{
char *val, *s, *t;
int token, len = 0;
pair c = NULL;
/* Read a dotted hostname... */
do {
/* Read a token, which should be an identifier. */
token = next_token(&val, cfile);
if (!is_identifier(token) && token != TOK_NUMBER) {
parse_warn("expecting an identifier in hostname");
skip_to_semi(cfile);
return (NULL);
}
/* Store this identifier... */
s = strdup(val);
if (s == NULL)
error("can't allocate temp space for hostname.");
c = cons((caddr_t) s, c);
len += strlen(s) + 1;
/*
* Look for a dot; if it's there, keep going, otherwise
* we're done.
*/
token = peek_token(&val, cfile);
if (token == '.')
token = next_token(&val, cfile);
} while (token == '.');
/* Assemble the hostname together into a string. */
if (!(s = malloc(len)))
error("can't allocate space for hostname.");
t = s + len;
*--t = '\0';
while (c) {
pair cdr = c->cdr;
int l = strlen((char *)c->car);
t -= l;
memcpy(t, (char *)c->car, l);
/* Free up temp space. */
free(c->car);
free(c);
c = cdr;
if (t != s)
*--t = '.';
}
return (s);
}
/*
* hardware-parameter :== HARDWARE ETHERNET csns SEMI
* csns :== NUMBER | csns COLON NUMBER
*/
void
parse_hardware_param(FILE *cfile, struct hardware *hardware)
{
char *val;
int token, hlen;
unsigned char *t;
token = next_token(&val, cfile);
switch (token) {
case TOK_ETHERNET:
hardware->htype = HTYPE_ETHER;
break;
case TOK_TOKEN_RING:
hardware->htype = HTYPE_IEEE802;
break;
case TOK_FDDI:
hardware->htype = HTYPE_FDDI;
break;
case TOK_IPSEC_TUNNEL:
hardware->htype = HTYPE_IPSEC_TUNNEL;
break;
default:
parse_warn("expecting a network hardware type");
skip_to_semi(cfile);
return;
}
/*
* Parse the hardware address information. Technically, it
* would make a lot of sense to restrict the length of the data
* we'll accept here to the length of a particular hardware
* address type. Unfortunately, there are some broken clients
* out there that put bogus data in the chaddr buffer, and we
* accept that data in the lease file rather than simply failing
* on such clients. Yuck.
*/
hlen = 0;
t = parse_numeric_aggregate(cfile, NULL, &hlen, ':', 16, 8);
if (!t)
return;
if (hlen > sizeof(hardware->haddr)) {
free(t);
parse_warn("hardware address too long");
} else {
hardware->hlen = hlen;
memcpy((unsigned char *)&hardware->haddr[0], t, hardware->hlen);
if (hlen < sizeof(hardware->haddr))
memset(&hardware->haddr[hlen], 0,
sizeof(hardware->haddr) - hlen);
free(t);
}
token = next_token(&val, cfile);
if (token != ';') {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
}
}
/*
* lease-time :== NUMBER SEMI
*/
void
parse_lease_time(FILE *cfile, time_t *timep)
{
char *val;
uint32_t value;
int token;
token = next_token(&val, cfile);
if (token != TOK_NUMBER) {
parse_warn("Expecting numeric lease time");
skip_to_semi(cfile);
return;
}
convert_num((unsigned char *)&value, val, 10, 32);
/* Unswap the number - convert_num returns stuff in NBO. */
*timep = ntohl(value); /* XXX */
parse_semi(cfile);
}
/*
* No BNF for numeric aggregates - that's defined by the caller. What
* this function does is to parse a sequence of numbers separated by the
* token specified in separator. If max is zero, any number of numbers
* will be parsed; otherwise, exactly max numbers are expected. Base
* and size tell us how to internalize the numbers once they've been
* tokenized.
*/
unsigned char *
parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max,
int separator, int base, int size)
{
char *val, *t;
int token, count = 0;
unsigned char *bufp = buf, *s = NULL;
pair c = NULL;
if (!bufp && *max) {
bufp = malloc(*max * size / 8);
if (!bufp)
error("can't allocate space for numeric aggregate");
} else
s = bufp;
do {
if (count) {
token = peek_token(&val, cfile);
if (token != separator) {
if (!*max)
break;
if (token != '{' && token != '}')
token = next_token(&val, cfile);
parse_warn("too few numbers.");
if (token != ';')
skip_to_semi(cfile);
return (NULL);
}
token = next_token(&val, cfile);
}
token = next_token(&val, cfile);
if (token == EOF) {
parse_warn("unexpected end of file");
break;
}
/* Allow NUMBER_OR_NAME if base is 16. */
if (token != TOK_NUMBER &&
(base != 16 || token != TOK_NUMBER_OR_NAME)) {
parse_warn("expecting numeric value.");
skip_to_semi(cfile);
return (NULL);
}
/*
* If we can, convert the number now; otherwise, build a
* linked list of all the numbers.
*/
if (s) {
convert_num(s, val, base, size);
s += size / 8;
} else {
t = strdup(val);
if (t == NULL)
error("no temp space for number.");
c = cons(t, c);
}
} while (++count != *max);
/* If we had to cons up a list, convert it now. */
if (c) {
bufp = malloc(count * size / 8);
if (!bufp)
error("can't allocate space for numeric aggregate.");
s = bufp + count - size / 8;
*max = count;
}
while (c) {
pair cdr = c->cdr;
convert_num(s, (char *)c->car, base, size);
s -= size / 8;
/* Free up temp space. */
free(c->car);
free(c);
c = cdr;
}
return (bufp);
}
void
convert_num(unsigned char *buf, char *str, int base, int size)
{
int negative = 0, tval, max;
u_int32_t val = 0;
char *ptr = str;
if (*ptr == '-') {
negative = 1;
ptr++;
}
/* If base wasn't specified, figure it out from the data. */
if (!base) {
if (ptr[0] == '0') {
if (ptr[1] == 'x') {
base = 16;
ptr += 2;
} else if (isascii(ptr[1]) && isdigit(ptr[1])) {
base = 8;
ptr += 1;
} else
base = 10;
} else
base = 10;
}
do {
tval = *ptr++;
/* XXX assumes ASCII... */
if (tval >= 'a')
tval = tval - 'a' + 10;
else if (tval >= 'A')
tval = tval - 'A' + 10;
else if (tval >= '0')
tval -= '0';
else {
warning("Bogus number: %s.", str);
break;
}
if (tval >= base) {
warning("Bogus number: %s: digit %d not in base %d",
str, tval, base);
break;
}
val = val * base + tval;
} while (*ptr);
if (negative)
max = (1 << (size - 1));
else
max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
if (val > max) {
switch (base) {
case 8:
warning("value %s%o exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
case 16:
warning("value %s%x exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
default:
warning("value %s%u exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
}
}
if (negative) {
switch (size) {
case 8:
*buf = -(unsigned long)val;
break;
case 16:
putShort(buf, -(unsigned long)val);
break;
case 32:
putLong(buf, -(unsigned long)val);
break;
default:
warning("Unexpected integer size: %d", size);
break;
}
} else {
switch (size) {
case 8:
*buf = (u_int8_t)val;
break;
case 16:
putUShort(buf, (u_int16_t)val);
break;
case 32:
putULong(buf, val);
break;
default:
warning("Unexpected integer size: %d", size);
break;
}
}
}
/*
* date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
* NUMBER COLON NUMBER COLON NUMBER UTC SEMI
*
* Dates are always in UTC; first number is day of week; next is
* year/month/day; next is hours:minutes:seconds on a 24-hour
* clock.
*/
time_t
parse_date(FILE *cfile)
{
struct tm tm;
char timestr[26]; /* "w yyyy/mm/dd hh:mm:ss UTC" */
char *val, *p;
size_t n;
time_t guess;
int token;
memset(timestr, 0, sizeof(timestr));
do {
token = peek_token(NULL, cfile);
switch (token) {
case TOK_NAME:
case TOK_NUMBER:
case '/':
case ':':
token = next_token(&val, cfile);
n = strlcat(timestr, val, sizeof(timestr));
if (n >= sizeof(timestr)) {
/* XXX Will break after year 9999! */
parse_warn("time string too long");
skip_to_semi(cfile);
return (0);
}
break;
case';':
break;
default:
parse_warn("invalid time string");
skip_to_semi(cfile);
return (0);
}
} while (token != ';');
parse_semi(cfile);
memset(&tm, 0, sizeof(tm)); /* 'cuz strptime ignores tm_isdt. */
p = strptime(timestr, DB_TIMEFMT, &tm);
if (p == NULL || *p != '\0') {
p = strptime(timestr, OLD_DB_TIMEFMT, &tm);
if (p == NULL || *p != '\0') {
parse_warn("unparseable time string");
return (0);
}
}
guess = timegm(&tm);
if (guess == -1) {
parse_warn("time could not be represented");
return (0);
}
return (guess);
}
|