1 /* $OpenBSD: conflex.c,v 1.7 2004/09/15 19:02:38 deraadt Exp $ */
3 /* Lexical scanner for dhcpd config file... */
6 * SPDX-License-Identifier: BSD-3-Clause
8 * Copyright (c) 1995, 1996, 1997 The Internet Software Consortium.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of The Internet Software Consortium nor the names
21 * of its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
25 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * This software has been written for the Internet Software Consortium
39 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
40 * Enterprises. To learn more about the Internet Software Consortium,
41 * see ``http://www.vix.com/isc''. To learn more about Vixie
42 * Enterprises, see ``http://www.vix.com''.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
56 static char *prev_line;
57 static char *cur_line;
61 static char line1[81];
62 static char line2[81];
70 static char tokbuf[1500];
72 static int get_char(FILE *);
73 static int get_token(FILE *);
74 static void skip_to_eol(FILE *);
75 static int read_string(FILE *);
76 static int read_number(int, FILE *);
77 static int read_num_or_name(int, FILE *);
78 static int intern(char *, int);
81 new_parse(const char *name)
87 token_line = cur_line;
88 cur_line[0] = prev_line[0] = 0;
89 warnings_occurred = 0;
98 if (cur_line == line1) {
108 } else if (c != EOF) {
109 if (lpos < sizeof(line1)) {
110 cur_line[lpos - 1] = c;
121 get_token(FILE *cfile)
133 if (!(c == '\n' && eol_token) && isascii(c) && isspace(c))
142 ttok = read_string(cfile);
145 if ((isascii(c) && isdigit(c)) || c == '-') {
148 ttok = read_number(c, cfile);
150 } else if (isascii(c) && isalpha(c)) {
153 ttok = read_num_or_name(c, cfile);
169 next_token(char **rval, FILE *cfile)
174 if (lexline != tline)
175 token_line = cur_line;
181 rv = get_token(cfile);
182 token_line = cur_line;
191 peek_token(char **rval, FILE *cfile)
198 token = get_token(cfile);
199 if (lexline != tline)
200 token_line = prev_line;
215 skip_to_eol(FILE *cfile)
229 read_string(FILE *cfile)
234 for (i = 0; i < sizeof(tokbuf); i++) {
237 parse_warn("eof in string constant");
244 } else if (c == '\\')
252 * Normally, I'd feel guilty about this, but we're talking about
253 * strings that'll fit in a DHCP packet here...
255 if (i == sizeof(tokbuf)) {
256 parse_warn("string constant larger than internal buffer");
265 read_number(int c, FILE *cfile)
267 int seenx = 0, _token = NUMBER;
271 for (; i < sizeof(tokbuf); i++) {
273 if (!seenx && c == 'x')
275 else if (!isascii(c) || !isxdigit(c)) {
282 if (i == sizeof(tokbuf)) {
283 parse_warn("numeric token larger than internal buffer");
293 read_num_or_name(int c, FILE *cfile)
296 int rv = NUMBER_OR_NAME;
299 for (; i < sizeof(tokbuf); i++) {
301 if (!isascii(c) || (c != '-' && c != '_' && !isalnum(c))) {
310 if (i == sizeof(tokbuf)) {
311 parse_warn("token larger than internal buffer");
317 return (intern(tval, rv));
321 intern(char *atom, int dfv)
323 if (!isascii(atom[0]))
326 switch (tolower(atom[0])) {
328 if (!strcasecmp(atom + 1, "lways-reply-rfc1048"))
329 return (ALWAYS_REPLY_RFC1048);
330 if (!strcasecmp(atom + 1, "ppend"))
332 if (!strcasecmp(atom + 1, "llow"))
334 if (!strcasecmp(atom + 1, "lias"))
336 if (!strcasecmp(atom + 1, "bandoned"))
338 if (!strcasecmp(atom + 1, "uthoritative"))
339 return (AUTHORITATIVE);
342 if (!strcasecmp(atom + 1, "ackoff-cutoff"))
343 return (BACKOFF_CUTOFF);
344 if (!strcasecmp(atom + 1, "ootp"))
346 if (!strcasecmp(atom + 1, "ooting"))
348 if (!strcasecmp(atom + 1, "oot-unknown-clients"))
349 return (BOOT_UNKNOWN_CLIENTS);
352 if (!strcasecmp(atom + 1, "lass"))
354 if (!strcasecmp(atom + 1, "iaddr"))
356 if (!strcasecmp(atom + 1, "lient-identifier"))
357 return (CLIENT_IDENTIFIER);
358 if (!strcasecmp(atom + 1, "lient-hostname"))
359 return (CLIENT_HOSTNAME);
362 if (!strcasecmp(atom + 1, "omain"))
364 if (!strcasecmp(atom + 1, "eny"))
366 if (!strncasecmp(atom + 1, "efault", 6)) {
369 if (!strcasecmp(atom + 7, "-lease-time"))
370 return (DEFAULT_LEASE_TIME);
373 if (!strncasecmp(atom + 1, "ynamic-bootp", 12)) {
375 return (DYNAMIC_BOOTP);
376 if (!strcasecmp(atom + 13, "-lease-cutoff"))
377 return (DYNAMIC_BOOTP_LEASE_CUTOFF);
378 if (!strcasecmp(atom + 13, "-lease-length"))
379 return (DYNAMIC_BOOTP_LEASE_LENGTH);
384 if (!strcasecmp(atom + 1, "thernet"))
386 if (!strcasecmp(atom + 1, "nds"))
388 if (!strcasecmp(atom + 1, "xpire"))
392 if (!strcasecmp(atom + 1, "ilename"))
394 if (!strcasecmp(atom + 1, "ixed-address"))
396 if (!strcasecmp(atom + 1, "ddi"))
400 if (!strcasecmp(atom + 1, "iaddr"))
402 if (!strcasecmp(atom + 1, "roup"))
404 if (!strcasecmp(atom + 1, "et-lease-hostnames"))
405 return (GET_LEASE_HOSTNAMES);
408 if (!strcasecmp(atom + 1, "ost"))
410 if (!strcasecmp(atom + 1, "ardware"))
412 if (!strcasecmp(atom + 1, "ostname"))
416 if (!strcasecmp(atom + 1, "nitial-interval"))
417 return (INITIAL_INTERVAL);
418 if (!strcasecmp(atom + 1, "nterface"))
422 if (!strcasecmp(atom + 1, "ease"))
426 if (!strcasecmp(atom + 1, "ax-lease-time"))
427 return (MAX_LEASE_TIME);
428 if (!strncasecmp(atom + 1, "edi", 3)) {
429 if (!strcasecmp(atom + 4, "a"))
431 if (!strcasecmp(atom + 4, "um"))
437 if (!strcasecmp(atom + 1, "ameserver"))
439 if (!strcasecmp(atom + 1, "etmask"))
441 if (!strcasecmp(atom + 1, "ext-server"))
442 return (NEXT_SERVER);
443 if (!strcasecmp(atom + 1, "ot"))
447 if (!strcasecmp(atom + 1, "ption"))
449 if (!strcasecmp(atom + 1, "ne-lease-per-client"))
450 return (ONE_LEASE_PER_CLIENT);
453 if (!strcasecmp(atom + 1, "repend"))
455 if (!strcasecmp(atom + 1, "acket"))
459 if (!strcasecmp(atom + 1, "ange"))
461 if (!strcasecmp(atom + 1, "equest"))
463 if (!strcasecmp(atom + 1, "equire"))
465 if (!strcasecmp(atom + 1, "etry"))
467 if (!strcasecmp(atom + 1, "enew"))
469 if (!strcasecmp(atom + 1, "ebind"))
471 if (!strcasecmp(atom + 1, "eboot"))
473 if (!strcasecmp(atom + 1, "eject"))
477 if (!strcasecmp(atom + 1, "earch"))
479 if (!strcasecmp(atom + 1, "tarts"))
481 if (!strcasecmp(atom + 1, "iaddr"))
483 if (!strcasecmp(atom + 1, "ubnet"))
485 if (!strcasecmp(atom + 1, "hared-network"))
486 return (SHARED_NETWORK);
487 if (!strcasecmp(atom + 1, "erver-name"))
488 return (SERVER_NAME);
489 if (!strcasecmp(atom + 1, "erver-identifier"))
490 return (SERVER_IDENTIFIER);
491 if (!strcasecmp(atom + 1, "elect-timeout"))
492 return (SELECT_TIMEOUT);
493 if (!strcasecmp(atom + 1, "end"))
495 if (!strcasecmp(atom + 1, "cript"))
497 if (!strcasecmp(atom + 1, "upersede"))
501 if (!strcasecmp(atom + 1, "imestamp"))
503 if (!strcasecmp(atom + 1, "imeout"))
505 if (!strcasecmp(atom + 1, "oken-ring"))
509 if (!strncasecmp(atom + 1, "se", 2)) {
510 if (!strcasecmp(atom + 3, "r-class"))
512 if (!strcasecmp(atom + 3, "-host-decl-names"))
513 return (USE_HOST_DECL_NAMES);
514 if (!strcasecmp(atom + 3,
515 "-lease-addr-for-default-route"))
516 return (USE_LEASE_ADDR_FOR_DEFAULT_ROUTE);
519 if (!strcasecmp(atom + 1, "id"))
521 if (!strcasecmp(atom + 1, "nknown-clients"))
522 return (UNKNOWN_CLIENTS);
525 if (!strcasecmp(atom + 1, "endor-class"))
526 return (VENDOR_CLASS);
529 if (!strcasecmp(atom + 1, "iaddr"))