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
|
/* $OpenBSD: conflex.c,v 1.32 2015/05/18 17:51:21 krw Exp $ */
/* Lexical scanner for dhclient config file. */
/*
* Copyright (c) 1995, 1996, 1997 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"
#include <vis.h>
int lexline;
int lexchar;
char *token_line;
char *prev_line;
char *cur_line;
char *tlname;
static char line1[81];
static char line2[81];
static int lpos;
static int line;
static int tlpos;
static int tline;
static int token;
static int ugflag;
static char *tval;
static char tokbuf[1500];
static char visbuf[1500];
static int get_char(FILE *);
static int get_token(FILE *);
static void skip_to_eol(FILE *);
static int read_string(FILE *);
static int read_num_or_name(int, FILE *);
static int intern(char *, int);
void
new_parse(char *name)
{
/*
* Initialize all parsing state, as we are starting to parse a
* new file, 'name'.
*/
memset(line1, 0, sizeof(line1));
memset(line2, 0, sizeof(line2));
memset(tokbuf, 0, sizeof(tokbuf));
lpos = line = 1;
tlpos = tline = token = ugflag = 0;
tval = NULL;
lexline = lexchar = 0;
cur_line = line1;
prev_line = line2;
token_line = cur_line;
tlname = name;
warnings_occurred = 0;
}
static int
get_char(FILE *cfile)
{
int c = getc(cfile);
if (!ugflag) {
if (c == '\n') {
if (cur_line == line1) {
cur_line = line2;
prev_line = line1;
} else {
cur_line = line1;
prev_line = line2;
}
line++;
lpos = 1;
cur_line[0] = 0;
} else if (c != EOF) {
if (lpos < sizeof(line1)) {
cur_line[lpos - 1] = c;
cur_line[lpos] = 0;
}
lpos++;
}
} else
ugflag = 0;
return (c);
}
static int
get_token(FILE *cfile)
{
int c, ttok;
static char tb[2];
int l, p, u;
u = ugflag;
do {
l = line;
p = lpos - u;
u = 0;
c = get_char(cfile);
if (isascii(c) && isspace(c))
continue;
if (c == '#') {
skip_to_eol(cfile);
continue;
}
lexline = l;
lexchar = p;
if (c == '"') {
ttok = read_string(cfile);
break;
} else if (c == '-' || (isascii(c) && isalnum(c))) {
ttok = read_num_or_name(c, cfile);
break;
} else {
tb[0] = c;
tb[1] = 0;
tval = tb;
ttok = c;
break;
}
} while (1);
return (ttok);
}
int
next_token(char **rval, FILE *cfile)
{
int rv;
if (token) {
if (lexline != tline)
token_line = cur_line;
lexchar = tlpos;
lexline = tline;
rv = token;
token = 0;
} else {
rv = get_token(cfile);
token_line = cur_line;
}
if (rval)
*rval = tval;
return (rv);
}
int
peek_token(char **rval, FILE *cfile)
{
int x;
if (!token) {
tlpos = lexchar;
tline = lexline;
token = get_token(cfile);
if (lexline != tline)
token_line = prev_line;
x = lexchar;
lexchar = tlpos;
tlpos = x;
x = lexline;
lexline = tline;
tline = x;
}
if (rval)
*rval = tval;
return (token);
}
static void
skip_to_eol(FILE *cfile)
{
int c;
do {
c = get_char(cfile);
if (c == EOF)
return;
if (c == '\n')
return;
} while (1);
}
static int
read_string(FILE *cfile)
{
int i, c, bs;
/*
* Read in characters until an un-escaped '"' is encountered. And
* then unvis the data that was read.
*/
bs = i = 0;
memset(visbuf, 0, sizeof(visbuf));
while ((c = get_char(cfile)) != EOF) {
if (c == '"' && bs == 0)
break;
visbuf[i++] = c;
if (bs)
bs = 0;
else if (c == '\\')
bs = 1;
if (i == sizeof(visbuf) - 1)
break;
}
if (bs == 1)
visbuf[--i] = '\0';
i = strnunvis(tokbuf, visbuf, sizeof(tokbuf));
if (c == EOF)
parse_warn("eof in string constant");
else if (i == -1 || i >= sizeof(tokbuf))
parse_warn("string constant too long");
tval = tokbuf;
return (TOK_STRING);
}
static int
read_num_or_name(int c, FILE *cfile)
{
int i, rv, xdigits;
xdigits = isxdigit(c) ? 1 : 0;
tokbuf[0] = c;
for (i = 1; i < sizeof(tokbuf); i++) {
c = get_char(cfile);
if (!isascii(c) || (c != '-' && c != '_' && !isalnum(c))) {
ungetc(c, cfile);
ugflag = 1;
break;
}
if (isxdigit(c))
xdigits++;
tokbuf[i] = c;
}
if (i == sizeof(tokbuf)) {
parse_warn("token larger than internal buffer");
i--;
c = tokbuf[i];
if (isxdigit(c))
xdigits--;
}
tokbuf[i] = 0;
tval = tokbuf;
c = (unsigned int)tokbuf[0];
if (c == '-')
rv = TOK_NUMBER;
else
rv = intern(tval, TOK_NUMBER_OR_NAME);
if (rv == TOK_NUMBER_OR_NAME && xdigits != i)
rv = TOK_NAME;
return (rv);
}
static const struct keywords {
const char *k_name;
int k_val;
} keywords[] = {
{ "alias", TOK_ALIAS },
{ "append", TOK_APPEND },
{ "backoff-cutoff", TOK_BACKOFF_CUTOFF },
{ "bootp", TOK_BOOTP },
{ "default", TOK_DEFAULT },
{ "deny", TOK_DENY },
{ "ethernet", TOK_ETHERNET },
{ "expire", TOK_EXPIRE },
{ "filename", TOK_FILENAME },
{ "fixed-address", TOK_FIXED_ADDR },
{ "hardware", TOK_HARDWARE },
{ "ignore", TOK_IGNORE },
{ "initial-interval", TOK_INITIAL_INTERVAL },
{ "interface", TOK_INTERFACE },
{ "lease", TOK_LEASE },
{ "link-timeout", TOK_LINK_TIMEOUT },
{ "media", TOK_MEDIA },
{ "medium", TOK_MEDIUM },
{ "next-server", TOK_NEXT_SERVER },
{ "option", TOK_OPTION },
{ "prepend", TOK_PREPEND },
{ "rebind", TOK_REBIND },
{ "reboot", TOK_REBOOT },
{ "reject", TOK_REJECT },
{ "renew", TOK_RENEW },
{ "request", TOK_REQUEST },
{ "require", TOK_REQUIRE },
{ "retry", TOK_RETRY },
{ "select-timeout", TOK_SELECT_TIMEOUT },
{ "send", TOK_SEND },
{ "server-name", TOK_SERVER_NAME },
{ "supersede", TOK_SUPERSEDE },
{ "timeout", TOK_TIMEOUT }
};
int kw_cmp(const void *k, const void *e);
int
kw_cmp(const void *k, const void *e)
{
return (strcasecmp(k, ((const struct keywords *)e)->k_name));
}
static int
intern(char *atom, int dfv)
{
const struct keywords *p;
p = bsearch(atom, keywords, sizeof(keywords)/sizeof(keywords[0]),
sizeof(keywords[0]), kw_cmp);
if (p)
return (p->k_val);
return (dfv);
}
|