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$");
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);
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);
351 if (!strcasecmp(atom + 1, "lass"))
353 if (!strcasecmp(atom + 1, "iaddr"))
355 if (!strcasecmp(atom + 1, "lient-identifier"))
356 return (CLIENT_IDENTIFIER);
357 if (!strcasecmp(atom + 1, "lient-hostname"))
358 return (CLIENT_HOSTNAME);
361 if (!strcasecmp(atom + 1, "omain"))
363 if (!strcasecmp(atom + 1, "eny"))
365 if (!strncasecmp(atom + 1, "efault", 6)) {
368 if (!strcasecmp(atom + 7, "-lease-time"))
369 return (DEFAULT_LEASE_TIME);
372 if (!strncasecmp(atom + 1, "ynamic-bootp", 12)) {
374 return (DYNAMIC_BOOTP);
375 if (!strcasecmp(atom + 13, "-lease-cutoff"))
376 return (DYNAMIC_BOOTP_LEASE_CUTOFF);
377 if (!strcasecmp(atom + 13, "-lease-length"))
378 return (DYNAMIC_BOOTP_LEASE_LENGTH);
383 if (!strcasecmp(atom + 1, "thernet"))
385 if (!strcasecmp(atom + 1, "nds"))
387 if (!strcasecmp(atom + 1, "xpire"))
391 if (!strcasecmp(atom + 1, "ilename"))
393 if (!strcasecmp(atom + 1, "ixed-address"))
395 if (!strcasecmp(atom + 1, "ddi"))
399 if (!strcasecmp(atom + 1, "iaddr"))
401 if (!strcasecmp(atom + 1, "roup"))
403 if (!strcasecmp(atom + 1, "et-lease-hostnames"))
404 return (GET_LEASE_HOSTNAMES);
407 if (!strcasecmp(atom + 1, "ost"))
409 if (!strcasecmp(atom + 1, "ardware"))
411 if (!strcasecmp(atom + 1, "ostname"))
415 if (!strcasecmp(atom + 1, "nitial-interval"))
416 return (INITIAL_INTERVAL);
417 if (!strcasecmp(atom + 1, "nterface"))
421 if (!strcasecmp(atom + 1, "ease"))
425 if (!strcasecmp(atom + 1, "ax-lease-time"))
426 return (MAX_LEASE_TIME);
427 if (!strncasecmp(atom + 1, "edi", 3)) {
428 if (!strcasecmp(atom + 4, "a"))
430 if (!strcasecmp(atom + 4, "um"))
436 if (!strcasecmp(atom + 1, "ameserver"))
438 if (!strcasecmp(atom + 1, "etmask"))
440 if (!strcasecmp(atom + 1, "ext-server"))
441 return (NEXT_SERVER);
442 if (!strcasecmp(atom + 1, "ot"))
446 if (!strcasecmp(atom + 1, "ption"))
448 if (!strcasecmp(atom + 1, "ne-lease-per-client"))
449 return (ONE_LEASE_PER_CLIENT);
452 if (!strcasecmp(atom + 1, "repend"))
454 if (!strcasecmp(atom + 1, "acket"))
458 if (!strcasecmp(atom + 1, "ange"))
460 if (!strcasecmp(atom + 1, "equest"))
462 if (!strcasecmp(atom + 1, "equire"))
464 if (!strcasecmp(atom + 1, "etry"))
466 if (!strcasecmp(atom + 1, "enew"))
468 if (!strcasecmp(atom + 1, "ebind"))
470 if (!strcasecmp(atom + 1, "eboot"))
472 if (!strcasecmp(atom + 1, "eject"))
476 if (!strcasecmp(atom + 1, "earch"))
478 if (!strcasecmp(atom + 1, "tarts"))
480 if (!strcasecmp(atom + 1, "iaddr"))
482 if (!strcasecmp(atom + 1, "ubnet"))
484 if (!strcasecmp(atom + 1, "hared-network"))
485 return (SHARED_NETWORK);
486 if (!strcasecmp(atom + 1, "erver-name"))
487 return (SERVER_NAME);
488 if (!strcasecmp(atom + 1, "erver-identifier"))
489 return (SERVER_IDENTIFIER);
490 if (!strcasecmp(atom + 1, "elect-timeout"))
491 return (SELECT_TIMEOUT);
492 if (!strcasecmp(atom + 1, "end"))
494 if (!strcasecmp(atom + 1, "cript"))
496 if (!strcasecmp(atom + 1, "upersede"))
500 if (!strcasecmp(atom + 1, "imestamp"))
502 if (!strcasecmp(atom + 1, "imeout"))
504 if (!strcasecmp(atom + 1, "oken-ring"))
508 if (!strncasecmp(atom + 1, "se", 2)) {
509 if (!strcasecmp(atom + 3, "r-class"))
511 if (!strcasecmp(atom + 3, "-host-decl-names"))
512 return (USE_HOST_DECL_NAMES);
513 if (!strcasecmp(atom + 3,
514 "-lease-addr-for-default-route"))
515 return (USE_LEASE_ADDR_FOR_DEFAULT_ROUTE);
518 if (!strcasecmp(atom + 1, "id"))
520 if (!strcasecmp(atom + 1, "nknown-clients"))
521 return (UNKNOWN_CLIENTS);
524 if (!strcasecmp(atom + 1, "endor-class"))
525 return (VENDOR_CLASS);
528 if (!strcasecmp(atom + 1, "iaddr"))