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>
49 struct client_config top_level_config;
50 static struct interface_info *dummy_interfaces;
52 static char client_script_name[] = "/sbin/dhclient-script";
55 * client-conf-file :== client-declarations EOF
56 * client-declarations :== <nil>
57 * | client-declaration
58 * | client-declarations client-declaration
61 read_client_conf(void)
66 struct client_config *config;
68 new_parse(path_dhclient_conf);
70 /* Set up the initial dhcp option universe. */
71 initialize_universes();
73 /* Initialize the top level client configuration. */
74 memset(&top_level_config, 0, sizeof(top_level_config));
76 /* Set some defaults... */
77 top_level_config.vlan_pcp = 0;
78 top_level_config.timeout = 60;
79 top_level_config.select_interval = 0;
80 top_level_config.reboot_timeout = 10;
81 top_level_config.retry_interval = 300;
82 top_level_config.backoff_cutoff = 15;
83 top_level_config.initial_interval = 3;
84 top_level_config.bootp_policy = ACCEPT;
85 top_level_config.script_name = client_script_name;
86 top_level_config.requested_options
87 [top_level_config.requested_option_count++] = DHO_SUBNET_MASK;
88 top_level_config.requested_options
89 [top_level_config.requested_option_count++] = DHO_BROADCAST_ADDRESS;
90 top_level_config.requested_options
91 [top_level_config.requested_option_count++] = DHO_TIME_OFFSET;
92 top_level_config.requested_options
93 [top_level_config.requested_option_count++] = DHO_CLASSLESS_ROUTES;
94 top_level_config.requested_options
95 [top_level_config.requested_option_count++] = DHO_ROUTERS;
96 top_level_config.requested_options
97 [top_level_config.requested_option_count++] = DHO_DOMAIN_NAME;
98 top_level_config.requested_options
99 [top_level_config.requested_option_count++] =
100 DHO_DOMAIN_NAME_SERVERS;
101 top_level_config.requested_options
102 [top_level_config.requested_option_count++] = DHO_HOST_NAME;
103 top_level_config.requested_options
104 [top_level_config.requested_option_count++] = DHO_DOMAIN_SEARCH;
105 top_level_config.requested_options
106 [top_level_config.requested_option_count++] = DHO_INTERFACE_MTU;
108 if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
110 token = peek_token(&val, cfile);
113 parse_client_statement(cfile, NULL, &top_level_config);
115 token = next_token(&val, cfile); /* Clear the peek buffer */
120 * Set up state and config structures for clients that don't
121 * have per-interface configuration declarations.
125 ifi->client = malloc(sizeof(struct client_state));
127 error("no memory for client state.");
128 memset(ifi->client, 0, sizeof(*(ifi->client)));
130 if (!ifi->client->config) {
132 config = malloc(sizeof(struct client_config));
134 error("no memory for client config.");
135 memcpy(config, &top_level_config,
136 sizeof(top_level_config));
138 ifi->client->config = config;
141 return (!warnings_occurred);
145 * lease-file :== client-lease-statements EOF
146 * client-lease-statements :== <nil>
147 * | client-lease-statements LEASE client-lease-statement
150 read_client_leases(void)
156 new_parse(path_dhclient_db);
158 /* Open the lease file. If we can't open it, just return -
159 we can safely trust the server to remember our state. */
160 if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
163 token = next_token(&val, cfile);
166 if (token != LEASE) {
167 warning("Corrupt lease file - possible data loss!");
171 parse_client_lease_statement(cfile, 0);
178 * client-declaration :==
180 * DEFAULT option-decl |
181 * SUPERSEDE option-decl |
182 * PREPEND option-decl |
183 * APPEND option-decl |
184 * hardware-declaration |
185 * REQUEST option-list |
186 * REQUIRE option-list |
187 * IGNORE option-list |
191 * SELECT_TIMEOUT number |
193 * interface-declaration |
194 * LEASE client-lease-statement |
195 * ALIAS client-lease-statement
198 parse_client_statement(FILE *cfile, struct interface_info *ip,
199 struct client_config *config)
202 struct option *option;
205 switch (next_token(&val, cfile)) {
207 parse_option_decl(cfile, &config->send_options[0]);
210 option = parse_option_decl(cfile, &config->defaults[0]);
212 config->default_actions[option->code] = ACTION_DEFAULT;
215 option = parse_option_decl(cfile, &config->defaults[0]);
217 config->default_actions[option->code] =
221 option = parse_option_decl(cfile, &config->defaults[0]);
223 config->default_actions[option->code] = ACTION_APPEND;
226 option = parse_option_decl(cfile, &config->defaults[0]);
228 config->default_actions[option->code] = ACTION_PREPEND;
231 parse_string_list(cfile, &config->media, 1);
235 parse_hardware_param(cfile, &ip->hw_address);
237 parse_warn("hardware address parameter %s",
238 "not allowed here.");
243 config->requested_option_count =
244 parse_option_list(cfile, config->requested_options);
247 memset(config->required_options, 0,
248 sizeof(config->required_options));
249 parse_option_list(cfile, config->required_options);
252 parse_option_list(cfile, config->ignored_options);
255 parse_lease_time(cfile, &config->timeout);
258 parse_lease_time(cfile, &config->retry_interval);
261 parse_lease_time(cfile, &config->select_interval);
264 parse_lease_time(cfile, &config->reboot_timeout);
267 parse_lease_time(cfile, &tmp);
268 config->vlan_pcp = (u_int)tmp;
271 parse_lease_time(cfile, &config->backoff_cutoff);
273 case INITIAL_INTERVAL:
274 parse_lease_time(cfile, &config->initial_interval);
277 config->script_name = parse_string(cfile);
281 parse_warn("nested interface declaration.");
282 parse_interface_declaration(cfile, config);
285 parse_client_lease_statement(cfile, 1);
288 parse_client_lease_statement(cfile, 2);
291 parse_reject_statement(cfile, config);
297 parse_warn("expecting a statement.");
302 parse_X(FILE *cfile, u_int8_t *buf, unsigned max)
308 token = peek_token(&val, cfile);
309 if (token == NUMBER_OR_NAME || token == NUMBER) {
312 token = next_token(&val, cfile);
313 if (token != NUMBER && token != NUMBER_OR_NAME) {
314 parse_warn("expecting hexadecimal constant.");
318 convert_num(&buf[len], val, 16, 8);
320 parse_warn("hexadecimal constant too long.");
324 token = peek_token(&val, cfile);
326 token = next_token(&val, cfile);
327 } while (token == COLON);
329 } else if (token == STRING) {
330 token = next_token(&val, cfile);
333 parse_warn("string constant too long.");
337 memcpy(buf, val, len + 1);
339 parse_warn("expecting string or hexadecimal data");
347 * option-list :== option_name |
348 * option_list COMMA option_name
351 parse_option_list(FILE *cfile, u_int8_t *list)
359 token = next_token(&val, cfile);
360 if (!is_identifier(token)) {
361 parse_warn("expected option name.");
365 for (i = 0; i < 256; i++)
366 if (!strcasecmp(dhcp_options[i].name, val))
370 parse_warn("%s: unexpected option name.", val);
376 parse_warn("%s: too many options.", val);
380 token = next_token(&val, cfile);
381 } while (token == COMMA);
383 parse_warn("expecting semicolon.");
391 * interface-declaration :==
392 * INTERFACE string LBRACE client-declarations RBRACE
395 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
399 struct interface_info *ip;
401 token = next_token(&val, cfile);
402 if (token != STRING) {
403 parse_warn("expecting interface name (in quotes).");
408 ip = interface_or_dummy(val);
411 make_client_state(ip);
413 if (!ip->client->config)
414 make_client_config(ip, outer_config);
416 token = next_token(&val, cfile);
417 if (token != LBRACE) {
418 parse_warn("expecting left brace.");
424 token = peek_token(&val, cfile);
426 parse_warn("unterminated interface declaration.");
431 parse_client_statement(cfile, ip, ip->client->config);
433 token = next_token(&val, cfile);
436 struct interface_info *
437 interface_or_dummy(char *name)
439 struct interface_info *ip;
441 /* Find the interface (if any) that matches the name. */
442 if (!strcmp(ifi->name, name))
445 /* If it's not a real interface, see if it's on the dummy list. */
446 for (ip = dummy_interfaces; ip; ip = ip->next)
447 if (!strcmp(ip->name, name))
451 * If we didn't find an interface, make a dummy interface as a
454 ip = malloc(sizeof(*ip));
456 error("Insufficient memory to record interface %s", name);
457 memset(ip, 0, sizeof(*ip));
458 strlcpy(ip->name, name, IFNAMSIZ);
459 ip->next = dummy_interfaces;
460 dummy_interfaces = ip;
465 make_client_state(struct interface_info *ip)
467 ip->client = malloc(sizeof(*(ip->client)));
469 error("no memory for state on %s", ip->name);
470 memset(ip->client, 0, sizeof(*(ip->client)));
474 make_client_config(struct interface_info *ip, struct client_config *config)
476 ip->client->config = malloc(sizeof(struct client_config));
477 if (!ip->client->config)
478 error("no memory for config for %s", ip->name);
479 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
480 memcpy(ip->client->config, config, sizeof(*config));
484 * client-lease-statement :==
485 * RBRACE client-lease-declarations LBRACE
487 * client-lease-declarations :==
489 * client-lease-declaration |
490 * client-lease-declarations client-lease-declaration
493 parse_client_lease_statement(FILE *cfile, int is_static)
495 struct client_lease *lease, *lp, *pl;
496 struct interface_info *ip;
500 token = next_token(&val, cfile);
501 if (token != LBRACE) {
502 parse_warn("expecting left brace.");
507 lease = malloc(sizeof(struct client_lease));
509 error("no memory for lease.");
510 memset(lease, 0, sizeof(*lease));
511 lease->is_static = is_static;
516 token = peek_token(&val, cfile);
518 parse_warn("unterminated lease declaration.");
519 free_client_lease(lease);
524 parse_client_lease_declaration(cfile, lease, &ip);
526 token = next_token(&val, cfile);
528 /* If the lease declaration didn't include an interface
529 * declaration that we recognized, it's of no use to us.
532 free_client_lease(lease);
536 /* Make sure there's a client state structure... */
538 make_client_state(ip);
540 /* If this is an alias lease, it doesn't need to be sorted in. */
541 if (is_static == 2) {
542 ip->client->alias = lease;
547 * The new lease may supersede a lease that's not the active
548 * lease but is still on the lease list, so scan the lease list
549 * looking for a lease with the same address, and if we find it,
553 for (lp = ip->client->leases; lp; lp = lp->next) {
554 if (lp->address.len == lease->address.len &&
555 !memcmp(lp->address.iabuf, lease->address.iabuf,
556 lease->address.len)) {
560 ip->client->leases = lp->next;
561 free_client_lease(lp);
567 * If this is a preloaded lease, just put it on the list of
568 * recorded leases - don't make it the active lease.
571 lease->next = ip->client->leases;
572 ip->client->leases = lease;
577 * The last lease in the lease file on a particular interface is
578 * the active lease for that interface. Of course, we don't
579 * know what the last lease in the file is until we've parsed
580 * the whole file, so at this point, we assume that the lease we
581 * just parsed is the active lease for its interface. If
582 * there's already an active lease for the interface, and this
583 * lease is for the same ip address, then we just toss the old
584 * active lease and replace it with this one. If this lease is
585 * for a different address, then if the old active lease has
586 * expired, we dump it; if not, we put it on the list of leases
587 * for this interface which are still valid but no longer
590 if (ip->client->active) {
591 if (ip->client->active->expiry < cur_time)
592 free_client_lease(ip->client->active);
593 else if (ip->client->active->address.len ==
594 lease->address.len &&
595 !memcmp(ip->client->active->address.iabuf,
596 lease->address.iabuf, lease->address.len))
597 free_client_lease(ip->client->active);
599 ip->client->active->next = ip->client->leases;
600 ip->client->leases = ip->client->active;
603 ip->client->active = lease;
609 * client-lease-declaration :==
612 * FIXED_ADDR ip_address |
614 * SERVER_NAME string |
615 * OPTION option-decl |
621 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
622 struct interface_info **ipp)
626 struct interface_info *ip;
628 switch (next_token(&val, cfile)) {
633 token = next_token(&val, cfile);
634 if (token != STRING) {
635 parse_warn("expecting interface name (in quotes).");
639 ip = interface_or_dummy(val);
643 if (!parse_ip_addr(cfile, &lease->address))
647 parse_string_list(cfile, &lease->medium, 0);
650 lease->filename = parse_string(cfile);
653 if (!parse_ip_addr(cfile, &lease->nextserver))
657 lease->server_name = parse_string(cfile);
660 lease->renewal = parse_date(cfile);
663 lease->rebind = parse_date(cfile);
666 lease->expiry = parse_date(cfile);
669 parse_option_decl(cfile, lease->options);
672 parse_warn("expecting lease declaration.");
676 token = next_token(&val, cfile);
678 parse_warn("expecting semicolon.");
684 parse_option_decl(FILE *cfile, struct option_data *options)
689 u_int8_t hunkbuf[1024];
693 struct universe *universe;
694 struct option *option;
695 struct iaddr ip_addr;
700 token = next_token(&val, cfile);
701 if (!is_identifier(token)) {
702 parse_warn("expecting identifier after option keyword.");
707 if ((vendor = strdup(val)) == NULL)
708 error("no memory for vendor information.");
710 token = peek_token(&val, cfile);
712 /* Go ahead and take the DOT token... */
713 token = next_token(&val, cfile);
715 /* The next token should be an identifier... */
716 token = next_token(&val, cfile);
717 if (!is_identifier(token)) {
718 parse_warn("expecting identifier after '.'");
725 /* Look up the option name hash table for the specified
727 universe = ((struct universe *)hash_lookup(&universe_hash,
728 (unsigned char *)vendor, 0));
729 /* If it's not there, we can't parse the rest of the
732 parse_warn("no vendor named %s.", vendor);
738 /* Use the default hash table, which contains all the
739 standard dhcp option names. */
741 universe = &dhcp_universe;
744 /* Look up the actual option info... */
745 option = (struct option *)hash_lookup(universe->hash,
746 (unsigned char *)val, 0);
748 /* If we didn't get an option structure, it's an undefined option. */
751 parse_warn("no option named %s", val);
753 parse_warn("no option named %s for vendor %s",
760 /* Free the initial identifier token. */
763 /* Parse the option data... */
765 for (fmt = option->format; *fmt; fmt++) {
770 len = parse_X(cfile, &hunkbuf[hunkix],
771 sizeof(hunkbuf) - hunkix);
774 case 't': /* Text string... */
775 token = next_token(&val, cfile);
776 if (token != STRING) {
777 parse_warn("expecting string.");
782 if (hunkix + len + 1 > sizeof(hunkbuf)) {
783 parse_warn("option data buffer %s",
788 memcpy(&hunkbuf[hunkix], val, len + 1);
792 case 'I': /* IP address. */
793 if (!parse_ip_addr(cfile, &ip_addr))
798 if (hunkix + len > sizeof(hunkbuf)) {
799 parse_warn("option data buffer "
804 memcpy(&hunkbuf[hunkix], dp, len);
807 case 'L': /* Unsigned 32-bit integer... */
808 case 'l': /* Signed 32-bit integer... */
809 token = next_token(&val, cfile);
810 if (token != NUMBER) {
812 parse_warn("expecting number.");
817 convert_num(buf, val, 0, 32);
821 case 's': /* Signed 16-bit integer. */
822 case 'S': /* Unsigned 16-bit integer. */
823 token = next_token(&val, cfile);
826 convert_num(buf, val, 0, 16);
830 case 'b': /* Signed 8-bit integer. */
831 case 'B': /* Unsigned 8-bit integer. */
832 token = next_token(&val, cfile);
835 convert_num(buf, val, 0, 8);
839 case 'f': /* Boolean flag. */
840 token = next_token(&val, cfile);
841 if (!is_identifier(token)) {
842 parse_warn("expecting identifier.");
848 if (!strcasecmp(val, "true") ||
849 !strcasecmp(val, "on"))
851 else if (!strcasecmp(val, "false") ||
852 !strcasecmp(val, "off"))
855 parse_warn("expecting boolean.");
862 warning("Bad format %c in parse_option_param.",
868 token = next_token(&val, cfile);
869 } while (*fmt == 'A' && token == COMMA);
872 parse_warn("semicolon expected.");
877 options[option->code].data = malloc(hunkix + nul_term);
878 if (!options[option->code].data)
879 error("out of memory allocating option data.");
880 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
881 options[option->code].len = hunkix;
886 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
891 struct string_list *cur, *tmp;
893 /* Find the last medium in the media list. */
895 for (cur = *lp; cur->next; cur = cur->next)
901 token = next_token(&val, cfile);
902 if (token != STRING) {
903 parse_warn("Expecting media options.");
908 valsize = strlen(val) + 1;
909 tmp = new_string_list(valsize);
911 error("no memory for string list entry.");
912 memcpy(tmp->string, val, valsize);
915 /* Store this medium at the end of the media list. */
922 token = next_token(&val, cfile);
923 } while (multiple && token == COMMA);
926 parse_warn("expecting semicolon.");
932 parse_reject_statement(FILE *cfile, struct client_config *config)
937 struct iaddrlist *list;
940 if (!parse_ip_addr(cfile, &addr)) {
941 parse_warn("expecting IP address.");
946 list = malloc(sizeof(struct iaddrlist));
948 error("no memory for reject list!");
951 list->next = config->reject_list;
952 config->reject_list = list;
954 token = next_token(&val, cfile);
955 } while (token == COMMA);
958 parse_warn("expecting semicolon.");