1 /* $OpenBSD: clparse.c,v 1.18 2004/09/15 18:15:18 henning Exp $ */
3 /* Parser for dhclient config and lease files... */
6 * SPDX-License-Identifier: BSD-3-Clause
8 * Copyright (c) 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$");
51 struct client_config top_level_config;
52 struct interface_info *dummy_interfaces;
53 extern struct interface_info *ifi;
55 char client_script_name[] = "/sbin/dhclient-script";
58 * client-conf-file :== client-declarations EOF
59 * client-declarations :== <nil>
60 * | client-declaration
61 * | client-declarations client-declaration
64 read_client_conf(void)
69 struct client_config *config;
71 new_parse(path_dhclient_conf);
73 /* Set up the initial dhcp option universe. */
74 initialize_universes();
76 /* Initialize the top level client configuration. */
77 memset(&top_level_config, 0, sizeof(top_level_config));
79 /* Set some defaults... */
80 top_level_config.timeout = 60;
81 top_level_config.select_interval = 0;
82 top_level_config.reboot_timeout = 10;
83 top_level_config.retry_interval = 300;
84 top_level_config.backoff_cutoff = 15;
85 top_level_config.initial_interval = 3;
86 top_level_config.bootp_policy = ACCEPT;
87 top_level_config.script_name = client_script_name;
88 top_level_config.requested_options
89 [top_level_config.requested_option_count++] = DHO_SUBNET_MASK;
90 top_level_config.requested_options
91 [top_level_config.requested_option_count++] = DHO_BROADCAST_ADDRESS;
92 top_level_config.requested_options
93 [top_level_config.requested_option_count++] = DHO_TIME_OFFSET;
94 top_level_config.requested_options
95 [top_level_config.requested_option_count++] = DHO_CLASSLESS_ROUTES;
96 top_level_config.requested_options
97 [top_level_config.requested_option_count++] = DHO_ROUTERS;
98 top_level_config.requested_options
99 [top_level_config.requested_option_count++] = DHO_DOMAIN_NAME;
100 top_level_config.requested_options
101 [top_level_config.requested_option_count++] =
102 DHO_DOMAIN_NAME_SERVERS;
103 top_level_config.requested_options
104 [top_level_config.requested_option_count++] = DHO_HOST_NAME;
105 top_level_config.requested_options
106 [top_level_config.requested_option_count++] = DHO_DOMAIN_SEARCH;
107 top_level_config.requested_options
108 [top_level_config.requested_option_count++] = DHO_INTERFACE_MTU;
110 if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
112 token = peek_token(&val, cfile);
115 parse_client_statement(cfile, NULL, &top_level_config);
117 token = next_token(&val, cfile); /* Clear the peek buffer */
122 * Set up state and config structures for clients that don't
123 * have per-interface configuration declarations.
127 ifi->client = malloc(sizeof(struct client_state));
129 error("no memory for client state.");
130 memset(ifi->client, 0, sizeof(*(ifi->client)));
132 if (!ifi->client->config) {
134 config = malloc(sizeof(struct client_config));
136 error("no memory for client config.");
137 memcpy(config, &top_level_config,
138 sizeof(top_level_config));
140 ifi->client->config = config;
143 return (!warnings_occurred);
147 * lease-file :== client-lease-statements EOF
148 * client-lease-statements :== <nil>
149 * | client-lease-statements LEASE client-lease-statement
152 read_client_leases(void)
158 new_parse(path_dhclient_db);
160 /* Open the lease file. If we can't open it, just return -
161 we can safely trust the server to remember our state. */
162 if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
165 token = next_token(&val, cfile);
168 if (token != LEASE) {
169 warning("Corrupt lease file - possible data loss!");
173 parse_client_lease_statement(cfile, 0);
180 * client-declaration :==
182 * DEFAULT option-decl |
183 * SUPERSEDE option-decl |
184 * PREPEND option-decl |
185 * APPEND option-decl |
186 * hardware-declaration |
187 * REQUEST option-list |
188 * REQUIRE option-list |
192 * SELECT_TIMEOUT number |
194 * interface-declaration |
195 * LEASE client-lease-statement |
196 * ALIAS client-lease-statement
199 parse_client_statement(FILE *cfile, struct interface_info *ip,
200 struct client_config *config)
204 struct option *option;
206 switch (next_token(&val, cfile)) {
208 parse_option_decl(cfile, &config->send_options[0]);
211 option = parse_option_decl(cfile, &config->defaults[0]);
213 config->default_actions[option->code] = ACTION_DEFAULT;
216 option = parse_option_decl(cfile, &config->defaults[0]);
218 config->default_actions[option->code] =
222 option = parse_option_decl(cfile, &config->defaults[0]);
224 config->default_actions[option->code] = ACTION_APPEND;
227 option = parse_option_decl(cfile, &config->defaults[0]);
229 config->default_actions[option->code] = ACTION_PREPEND;
232 parse_string_list(cfile, &config->media, 1);
236 parse_hardware_param(cfile, &ip->hw_address);
238 parse_warn("hardware address parameter %s",
239 "not allowed here.");
244 config->requested_option_count =
245 parse_option_list(cfile, config->requested_options);
248 memset(config->required_options, 0,
249 sizeof(config->required_options));
250 parse_option_list(cfile, config->required_options);
253 parse_lease_time(cfile, &config->timeout);
256 parse_lease_time(cfile, &config->retry_interval);
259 parse_lease_time(cfile, &config->select_interval);
262 parse_lease_time(cfile, &config->reboot_timeout);
265 parse_lease_time(cfile, &config->backoff_cutoff);
267 case INITIAL_INTERVAL:
268 parse_lease_time(cfile, &config->initial_interval);
271 config->script_name = parse_string(cfile);
275 parse_warn("nested interface declaration.");
276 parse_interface_declaration(cfile, config);
279 parse_client_lease_statement(cfile, 1);
282 parse_client_lease_statement(cfile, 2);
285 parse_reject_statement(cfile, config);
288 parse_warn("expecting a statement.");
292 token = next_token(&val, cfile);
294 parse_warn("semicolon expected.");
300 parse_X(FILE *cfile, u_int8_t *buf, int max)
306 token = peek_token(&val, cfile);
307 if (token == NUMBER_OR_NAME || token == NUMBER) {
310 token = next_token(&val, cfile);
311 if (token != NUMBER && token != NUMBER_OR_NAME) {
312 parse_warn("expecting hexadecimal constant.");
316 convert_num(&buf[len], val, 16, 8);
318 parse_warn("hexadecimal constant too long.");
322 token = peek_token(&val, cfile);
324 token = next_token(&val, cfile);
325 } while (token == COLON);
327 } else if (token == STRING) {
328 token = next_token(&val, cfile);
331 parse_warn("string constant too long.");
335 memcpy(buf, val, len + 1);
337 parse_warn("expecting string or hexadecimal data");
345 * option-list :== option_name |
346 * option_list COMMA option_name
349 parse_option_list(FILE *cfile, u_int8_t *list)
357 token = next_token(&val, cfile);
358 if (!is_identifier(token)) {
359 parse_warn("expected option name.");
363 for (i = 0; i < 256; i++)
364 if (!strcasecmp(dhcp_options[i].name, val))
368 parse_warn("%s: unexpected option name.", val);
374 parse_warn("%s: too many options.", val);
378 token = next_token(&val, cfile);
379 } while (token == COMMA);
381 parse_warn("expecting semicolon.");
389 * interface-declaration :==
390 * INTERFACE string LBRACE client-declarations RBRACE
393 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
397 struct interface_info *ip;
399 token = next_token(&val, cfile);
400 if (token != STRING) {
401 parse_warn("expecting interface name (in quotes).");
406 ip = interface_or_dummy(val);
409 make_client_state(ip);
411 if (!ip->client->config)
412 make_client_config(ip, outer_config);
414 token = next_token(&val, cfile);
415 if (token != LBRACE) {
416 parse_warn("expecting left brace.");
422 token = peek_token(&val, cfile);
424 parse_warn("unterminated interface declaration.");
429 parse_client_statement(cfile, ip, ip->client->config);
431 token = next_token(&val, cfile);
434 struct interface_info *
435 interface_or_dummy(char *name)
437 struct interface_info *ip;
439 /* Find the interface (if any) that matches the name. */
440 if (!strcmp(ifi->name, name))
443 /* If it's not a real interface, see if it's on the dummy list. */
444 for (ip = dummy_interfaces; ip; ip = ip->next)
445 if (!strcmp(ip->name, name))
449 * If we didn't find an interface, make a dummy interface as a
452 ip = malloc(sizeof(*ip));
454 error("Insufficient memory to record interface %s", name);
455 memset(ip, 0, sizeof(*ip));
456 strlcpy(ip->name, name, IFNAMSIZ);
457 ip->next = dummy_interfaces;
458 dummy_interfaces = ip;
463 make_client_state(struct interface_info *ip)
465 ip->client = malloc(sizeof(*(ip->client)));
467 error("no memory for state on %s", ip->name);
468 memset(ip->client, 0, sizeof(*(ip->client)));
472 make_client_config(struct interface_info *ip, struct client_config *config)
474 ip->client->config = malloc(sizeof(struct client_config));
475 if (!ip->client->config)
476 error("no memory for config for %s", ip->name);
477 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
478 memcpy(ip->client->config, config, sizeof(*config));
482 * client-lease-statement :==
483 * RBRACE client-lease-declarations LBRACE
485 * client-lease-declarations :==
487 * client-lease-declaration |
488 * client-lease-declarations client-lease-declaration
491 parse_client_lease_statement(FILE *cfile, int is_static)
493 struct client_lease *lease, *lp, *pl;
494 struct interface_info *ip;
498 token = next_token(&val, cfile);
499 if (token != LBRACE) {
500 parse_warn("expecting left brace.");
505 lease = malloc(sizeof(struct client_lease));
507 error("no memory for lease.");
508 memset(lease, 0, sizeof(*lease));
509 lease->is_static = is_static;
514 token = peek_token(&val, cfile);
516 parse_warn("unterminated lease declaration.");
517 free_client_lease(lease);
522 parse_client_lease_declaration(cfile, lease, &ip);
524 token = next_token(&val, cfile);
526 /* If the lease declaration didn't include an interface
527 * declaration that we recognized, it's of no use to us.
530 free_client_lease(lease);
534 /* Make sure there's a client state structure... */
536 make_client_state(ip);
538 /* If this is an alias lease, it doesn't need to be sorted in. */
539 if (is_static == 2) {
540 ip->client->alias = lease;
545 * The new lease may supersede a lease that's not the active
546 * lease but is still on the lease list, so scan the lease list
547 * looking for a lease with the same address, and if we find it,
551 for (lp = ip->client->leases; lp; lp = lp->next) {
552 if (lp->address.len == lease->address.len &&
553 !memcmp(lp->address.iabuf, lease->address.iabuf,
554 lease->address.len)) {
558 ip->client->leases = lp->next;
559 free_client_lease(lp);
565 * If this is a preloaded lease, just put it on the list of
566 * recorded leases - don't make it the active lease.
569 lease->next = ip->client->leases;
570 ip->client->leases = lease;
575 * The last lease in the lease file on a particular interface is
576 * the active lease for that interface. Of course, we don't
577 * know what the last lease in the file is until we've parsed
578 * the whole file, so at this point, we assume that the lease we
579 * just parsed is the active lease for its interface. If
580 * there's already an active lease for the interface, and this
581 * lease is for the same ip address, then we just toss the old
582 * active lease and replace it with this one. If this lease is
583 * for a different address, then if the old active lease has
584 * expired, we dump it; if not, we put it on the list of leases
585 * for this interface which are still valid but no longer
588 if (ip->client->active) {
589 if (ip->client->active->expiry < cur_time)
590 free_client_lease(ip->client->active);
591 else if (ip->client->active->address.len ==
592 lease->address.len &&
593 !memcmp(ip->client->active->address.iabuf,
594 lease->address.iabuf, lease->address.len))
595 free_client_lease(ip->client->active);
597 ip->client->active->next = ip->client->leases;
598 ip->client->leases = ip->client->active;
601 ip->client->active = lease;
607 * client-lease-declaration :==
610 * FIXED_ADDR ip_address |
612 * SERVER_NAME string |
613 * OPTION option-decl |
619 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
620 struct interface_info **ipp)
624 struct interface_info *ip;
626 switch (next_token(&val, cfile)) {
631 token = next_token(&val, cfile);
632 if (token != STRING) {
633 parse_warn("expecting interface name (in quotes).");
637 ip = interface_or_dummy(val);
641 if (!parse_ip_addr(cfile, &lease->address))
645 parse_string_list(cfile, &lease->medium, 0);
648 lease->filename = parse_string(cfile);
651 if (!parse_ip_addr(cfile, &lease->nextserver))
655 lease->server_name = parse_string(cfile);
658 lease->renewal = parse_date(cfile);
661 lease->rebind = parse_date(cfile);
664 lease->expiry = parse_date(cfile);
667 parse_option_decl(cfile, lease->options);
670 parse_warn("expecting lease declaration.");
674 token = next_token(&val, cfile);
676 parse_warn("expecting semicolon.");
682 parse_option_decl(FILE *cfile, struct option_data *options)
687 u_int8_t hunkbuf[1024];
691 struct universe *universe;
692 struct option *option;
693 struct iaddr ip_addr;
698 token = next_token(&val, cfile);
699 if (!is_identifier(token)) {
700 parse_warn("expecting identifier after option keyword.");
705 if ((vendor = strdup(val)) == NULL)
706 error("no memory for vendor information.");
708 token = peek_token(&val, cfile);
710 /* Go ahead and take the DOT token... */
711 token = next_token(&val, cfile);
713 /* The next token should be an identifier... */
714 token = next_token(&val, cfile);
715 if (!is_identifier(token)) {
716 parse_warn("expecting identifier after '.'");
723 /* Look up the option name hash table for the specified
725 universe = ((struct universe *)hash_lookup(&universe_hash,
726 (unsigned char *)vendor, 0));
727 /* If it's not there, we can't parse the rest of the
730 parse_warn("no vendor named %s.", vendor);
736 /* Use the default hash table, which contains all the
737 standard dhcp option names. */
739 universe = &dhcp_universe;
742 /* Look up the actual option info... */
743 option = (struct option *)hash_lookup(universe->hash,
744 (unsigned char *)val, 0);
746 /* If we didn't get an option structure, it's an undefined option. */
749 parse_warn("no option named %s", val);
751 parse_warn("no option named %s for vendor %s",
758 /* Free the initial identifier token. */
761 /* Parse the option data... */
763 for (fmt = option->format; *fmt; fmt++) {
768 len = parse_X(cfile, &hunkbuf[hunkix],
769 sizeof(hunkbuf) - hunkix);
772 case 't': /* Text string... */
773 token = next_token(&val, cfile);
774 if (token != STRING) {
775 parse_warn("expecting string.");
780 if (hunkix + len + 1 > sizeof(hunkbuf)) {
781 parse_warn("option data buffer %s",
786 memcpy(&hunkbuf[hunkix], val, len + 1);
790 case 'I': /* IP address. */
791 if (!parse_ip_addr(cfile, &ip_addr))
796 if (hunkix + len > sizeof(hunkbuf)) {
797 parse_warn("option data buffer "
802 memcpy(&hunkbuf[hunkix], dp, len);
805 case 'L': /* Unsigned 32-bit integer... */
806 case 'l': /* Signed 32-bit integer... */
807 token = next_token(&val, cfile);
808 if (token != NUMBER) {
810 parse_warn("expecting number.");
815 convert_num(buf, val, 0, 32);
819 case 's': /* Signed 16-bit integer. */
820 case 'S': /* Unsigned 16-bit integer. */
821 token = next_token(&val, cfile);
824 convert_num(buf, val, 0, 16);
828 case 'b': /* Signed 8-bit integer. */
829 case 'B': /* Unsigned 8-bit integer. */
830 token = next_token(&val, cfile);
833 convert_num(buf, val, 0, 8);
837 case 'f': /* Boolean flag. */
838 token = next_token(&val, cfile);
839 if (!is_identifier(token)) {
840 parse_warn("expecting identifier.");
846 if (!strcasecmp(val, "true") ||
847 !strcasecmp(val, "on"))
849 else if (!strcasecmp(val, "false") ||
850 !strcasecmp(val, "off"))
853 parse_warn("expecting boolean.");
860 warning("Bad format %c in parse_option_param.",
866 token = next_token(&val, cfile);
867 } while (*fmt == 'A' && token == COMMA);
870 parse_warn("semicolon expected.");
875 options[option->code].data = malloc(hunkix + nul_term);
876 if (!options[option->code].data)
877 error("out of memory allocating option data.");
878 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
879 options[option->code].len = hunkix;
884 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
889 struct string_list *cur, *tmp;
891 /* Find the last medium in the media list. */
893 for (cur = *lp; cur->next; cur = cur->next)
899 token = next_token(&val, cfile);
900 if (token != STRING) {
901 parse_warn("Expecting media options.");
906 valsize = strlen(val) + 1;
907 tmp = new_string_list(valsize);
909 error("no memory for string list entry.");
910 memcpy(tmp->string, val, valsize);
913 /* Store this medium at the end of the media list. */
920 token = next_token(&val, cfile);
921 } while (multiple && token == COMMA);
924 parse_warn("expecting semicolon.");
930 parse_reject_statement(FILE *cfile, struct client_config *config)
935 struct iaddrlist *list;
938 if (!parse_ip_addr(cfile, &addr)) {
939 parse_warn("expecting IP address.");
944 list = malloc(sizeof(struct iaddrlist));
946 error("no memory for reject list!");
949 list->next = config->reject_list;
950 config->reject_list = list;
952 token = next_token(&val, cfile);
953 } while (token == COMMA);
956 parse_warn("expecting semicolon.");