1 /* $OpenBSD: clparse.c,v 1.18 2004/09/15 18:15:18 henning Exp $ */
3 /* Parser for dhclient config and lease files... */
6 * Copyright (c) 1997 The Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
49 struct client_config top_level_config;
50 struct interface_info *dummy_interfaces;
51 extern struct interface_info *ifi;
53 char client_script_name[] = "/sbin/dhclient-script";
56 * client-conf-file :== client-declarations EOF
57 * client-declarations :== <nil>
58 * | client-declaration
59 * | client-declarations client-declaration
62 read_client_conf(void)
67 struct client_config *config;
69 new_parse(path_dhclient_conf);
71 /* Set up the initial dhcp option universe. */
72 initialize_universes();
74 /* Initialize the top level client configuration. */
75 memset(&top_level_config, 0, sizeof(top_level_config));
77 /* Set some defaults... */
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 |
190 * SELECT_TIMEOUT number |
192 * interface-declaration |
193 * LEASE client-lease-statement |
194 * ALIAS client-lease-statement
197 parse_client_statement(FILE *cfile, struct interface_info *ip,
198 struct client_config *config)
202 struct option *option;
204 switch (next_token(&val, cfile)) {
206 parse_option_decl(cfile, &config->send_options[0]);
209 option = parse_option_decl(cfile, &config->defaults[0]);
211 config->default_actions[option->code] = ACTION_DEFAULT;
214 option = parse_option_decl(cfile, &config->defaults[0]);
216 config->default_actions[option->code] =
220 option = parse_option_decl(cfile, &config->defaults[0]);
222 config->default_actions[option->code] = ACTION_APPEND;
225 option = parse_option_decl(cfile, &config->defaults[0]);
227 config->default_actions[option->code] = ACTION_PREPEND;
230 parse_string_list(cfile, &config->media, 1);
234 parse_hardware_param(cfile, &ip->hw_address);
236 parse_warn("hardware address parameter %s",
237 "not allowed here.");
242 config->requested_option_count =
243 parse_option_list(cfile, config->requested_options);
246 memset(config->required_options, 0,
247 sizeof(config->required_options));
248 parse_option_list(cfile, config->required_options);
251 parse_lease_time(cfile, &config->timeout);
254 parse_lease_time(cfile, &config->retry_interval);
257 parse_lease_time(cfile, &config->select_interval);
260 parse_lease_time(cfile, &config->reboot_timeout);
263 parse_lease_time(cfile, &config->backoff_cutoff);
265 case INITIAL_INTERVAL:
266 parse_lease_time(cfile, &config->initial_interval);
269 config->script_name = parse_string(cfile);
273 parse_warn("nested interface declaration.");
274 parse_interface_declaration(cfile, config);
277 parse_client_lease_statement(cfile, 1);
280 parse_client_lease_statement(cfile, 2);
283 parse_reject_statement(cfile, config);
286 parse_warn("expecting a statement.");
290 token = next_token(&val, cfile);
292 parse_warn("semicolon expected.");
298 parse_X(FILE *cfile, u_int8_t *buf, int max)
304 token = peek_token(&val, cfile);
305 if (token == NUMBER_OR_NAME || token == NUMBER) {
308 token = next_token(&val, cfile);
309 if (token != NUMBER && token != NUMBER_OR_NAME) {
310 parse_warn("expecting hexadecimal constant.");
314 convert_num(&buf[len], val, 16, 8);
316 parse_warn("hexadecimal constant too long.");
320 token = peek_token(&val, cfile);
322 token = next_token(&val, cfile);
323 } while (token == COLON);
325 } else if (token == STRING) {
326 token = next_token(&val, cfile);
329 parse_warn("string constant too long.");
333 memcpy(buf, val, len + 1);
335 parse_warn("expecting string or hexadecimal data");
343 * option-list :== option_name |
344 * option_list COMMA option_name
347 parse_option_list(FILE *cfile, u_int8_t *list)
355 token = next_token(&val, cfile);
356 if (!is_identifier(token)) {
357 parse_warn("expected option name.");
361 for (i = 0; i < 256; i++)
362 if (!strcasecmp(dhcp_options[i].name, val))
366 parse_warn("%s: unexpected option name.", val);
372 parse_warn("%s: too many options.", val);
376 token = next_token(&val, cfile);
377 } while (token == COMMA);
379 parse_warn("expecting semicolon.");
387 * interface-declaration :==
388 * INTERFACE string LBRACE client-declarations RBRACE
391 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
395 struct interface_info *ip;
397 token = next_token(&val, cfile);
398 if (token != STRING) {
399 parse_warn("expecting interface name (in quotes).");
404 ip = interface_or_dummy(val);
407 make_client_state(ip);
409 if (!ip->client->config)
410 make_client_config(ip, outer_config);
412 token = next_token(&val, cfile);
413 if (token != LBRACE) {
414 parse_warn("expecting left brace.");
420 token = peek_token(&val, cfile);
422 parse_warn("unterminated interface declaration.");
427 parse_client_statement(cfile, ip, ip->client->config);
429 token = next_token(&val, cfile);
432 struct interface_info *
433 interface_or_dummy(char *name)
435 struct interface_info *ip;
437 /* Find the interface (if any) that matches the name. */
438 if (!strcmp(ifi->name, name))
441 /* If it's not a real interface, see if it's on the dummy list. */
442 for (ip = dummy_interfaces; ip; ip = ip->next)
443 if (!strcmp(ip->name, name))
447 * If we didn't find an interface, make a dummy interface as a
450 ip = malloc(sizeof(*ip));
452 error("Insufficient memory to record interface %s", name);
453 memset(ip, 0, sizeof(*ip));
454 strlcpy(ip->name, name, IFNAMSIZ);
455 ip->next = dummy_interfaces;
456 dummy_interfaces = ip;
461 make_client_state(struct interface_info *ip)
463 ip->client = malloc(sizeof(*(ip->client)));
465 error("no memory for state on %s", ip->name);
466 memset(ip->client, 0, sizeof(*(ip->client)));
470 make_client_config(struct interface_info *ip, struct client_config *config)
472 ip->client->config = malloc(sizeof(struct client_config));
473 if (!ip->client->config)
474 error("no memory for config for %s", ip->name);
475 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
476 memcpy(ip->client->config, config, sizeof(*config));
480 * client-lease-statement :==
481 * RBRACE client-lease-declarations LBRACE
483 * client-lease-declarations :==
485 * client-lease-declaration |
486 * client-lease-declarations client-lease-declaration
489 parse_client_lease_statement(FILE *cfile, int is_static)
491 struct client_lease *lease, *lp, *pl;
492 struct interface_info *ip;
496 token = next_token(&val, cfile);
497 if (token != LBRACE) {
498 parse_warn("expecting left brace.");
503 lease = malloc(sizeof(struct client_lease));
505 error("no memory for lease.");
506 memset(lease, 0, sizeof(*lease));
507 lease->is_static = is_static;
512 token = peek_token(&val, cfile);
514 parse_warn("unterminated lease declaration.");
515 free_client_lease(lease);
520 parse_client_lease_declaration(cfile, lease, &ip);
522 token = next_token(&val, cfile);
524 /* If the lease declaration didn't include an interface
525 * declaration that we recognized, it's of no use to us.
528 free_client_lease(lease);
532 /* Make sure there's a client state structure... */
534 make_client_state(ip);
536 /* If this is an alias lease, it doesn't need to be sorted in. */
537 if (is_static == 2) {
538 ip->client->alias = lease;
543 * The new lease may supersede a lease that's not the active
544 * lease but is still on the lease list, so scan the lease list
545 * looking for a lease with the same address, and if we find it,
549 for (lp = ip->client->leases; lp; lp = lp->next) {
550 if (lp->address.len == lease->address.len &&
551 !memcmp(lp->address.iabuf, lease->address.iabuf,
552 lease->address.len)) {
556 ip->client->leases = lp->next;
557 free_client_lease(lp);
563 * If this is a preloaded lease, just put it on the list of
564 * recorded leases - don't make it the active lease.
567 lease->next = ip->client->leases;
568 ip->client->leases = lease;
573 * The last lease in the lease file on a particular interface is
574 * the active lease for that interface. Of course, we don't
575 * know what the last lease in the file is until we've parsed
576 * the whole file, so at this point, we assume that the lease we
577 * just parsed is the active lease for its interface. If
578 * there's already an active lease for the interface, and this
579 * lease is for the same ip address, then we just toss the old
580 * active lease and replace it with this one. If this lease is
581 * for a different address, then if the old active lease has
582 * expired, we dump it; if not, we put it on the list of leases
583 * for this interface which are still valid but no longer
586 if (ip->client->active) {
587 if (ip->client->active->expiry < cur_time)
588 free_client_lease(ip->client->active);
589 else if (ip->client->active->address.len ==
590 lease->address.len &&
591 !memcmp(ip->client->active->address.iabuf,
592 lease->address.iabuf, lease->address.len))
593 free_client_lease(ip->client->active);
595 ip->client->active->next = ip->client->leases;
596 ip->client->leases = ip->client->active;
599 ip->client->active = lease;
605 * client-lease-declaration :==
608 * FIXED_ADDR ip_address |
610 * SERVER_NAME string |
611 * OPTION option-decl |
617 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
618 struct interface_info **ipp)
622 struct interface_info *ip;
624 switch (next_token(&val, cfile)) {
629 token = next_token(&val, cfile);
630 if (token != STRING) {
631 parse_warn("expecting interface name (in quotes).");
635 ip = interface_or_dummy(val);
639 if (!parse_ip_addr(cfile, &lease->address))
643 parse_string_list(cfile, &lease->medium, 0);
646 lease->filename = parse_string(cfile);
649 if (!parse_ip_addr(cfile, &lease->nextserver))
653 lease->server_name = parse_string(cfile);
656 lease->renewal = parse_date(cfile);
659 lease->rebind = parse_date(cfile);
662 lease->expiry = parse_date(cfile);
665 parse_option_decl(cfile, lease->options);
668 parse_warn("expecting lease declaration.");
672 token = next_token(&val, cfile);
674 parse_warn("expecting semicolon.");
680 parse_option_decl(FILE *cfile, struct option_data *options)
685 u_int8_t hunkbuf[1024];
689 struct universe *universe;
690 struct option *option;
691 struct iaddr ip_addr;
696 token = next_token(&val, cfile);
697 if (!is_identifier(token)) {
698 parse_warn("expecting identifier after option keyword.");
703 if ((vendor = strdup(val)) == NULL)
704 error("no memory for vendor information.");
706 token = peek_token(&val, cfile);
708 /* Go ahead and take the DOT token... */
709 token = next_token(&val, cfile);
711 /* The next token should be an identifier... */
712 token = next_token(&val, cfile);
713 if (!is_identifier(token)) {
714 parse_warn("expecting identifier after '.'");
721 /* Look up the option name hash table for the specified
723 universe = ((struct universe *)hash_lookup(&universe_hash,
724 (unsigned char *)vendor, 0));
725 /* If it's not there, we can't parse the rest of the
728 parse_warn("no vendor named %s.", vendor);
734 /* Use the default hash table, which contains all the
735 standard dhcp option names. */
737 universe = &dhcp_universe;
740 /* Look up the actual option info... */
741 option = (struct option *)hash_lookup(universe->hash,
742 (unsigned char *)val, 0);
744 /* If we didn't get an option structure, it's an undefined option. */
747 parse_warn("no option named %s", val);
749 parse_warn("no option named %s for vendor %s",
756 /* Free the initial identifier token. */
759 /* Parse the option data... */
761 for (fmt = option->format; *fmt; fmt++) {
766 len = parse_X(cfile, &hunkbuf[hunkix],
767 sizeof(hunkbuf) - hunkix);
770 case 't': /* Text string... */
771 token = next_token(&val, cfile);
772 if (token != STRING) {
773 parse_warn("expecting string.");
778 if (hunkix + len + 1 > sizeof(hunkbuf)) {
779 parse_warn("option data buffer %s",
784 memcpy(&hunkbuf[hunkix], val, len + 1);
788 case 'I': /* IP address. */
789 if (!parse_ip_addr(cfile, &ip_addr))
794 if (hunkix + len > sizeof(hunkbuf)) {
795 parse_warn("option data buffer "
800 memcpy(&hunkbuf[hunkix], dp, len);
803 case 'L': /* Unsigned 32-bit integer... */
804 case 'l': /* Signed 32-bit integer... */
805 token = next_token(&val, cfile);
806 if (token != NUMBER) {
808 parse_warn("expecting number.");
813 convert_num(buf, val, 0, 32);
817 case 's': /* Signed 16-bit integer. */
818 case 'S': /* Unsigned 16-bit integer. */
819 token = next_token(&val, cfile);
822 convert_num(buf, val, 0, 16);
826 case 'b': /* Signed 8-bit integer. */
827 case 'B': /* Unsigned 8-bit integer. */
828 token = next_token(&val, cfile);
831 convert_num(buf, val, 0, 8);
835 case 'f': /* Boolean flag. */
836 token = next_token(&val, cfile);
837 if (!is_identifier(token)) {
838 parse_warn("expecting identifier.");
844 if (!strcasecmp(val, "true") ||
845 !strcasecmp(val, "on"))
847 else if (!strcasecmp(val, "false") ||
848 !strcasecmp(val, "off"))
851 parse_warn("expecting boolean.");
858 warning("Bad format %c in parse_option_param.",
864 token = next_token(&val, cfile);
865 } while (*fmt == 'A' && token == COMMA);
868 parse_warn("semicolon expected.");
873 options[option->code].data = malloc(hunkix + nul_term);
874 if (!options[option->code].data)
875 error("out of memory allocating option data.");
876 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
877 options[option->code].len = hunkix;
882 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
887 struct string_list *cur, *tmp;
889 /* Find the last medium in the media list. */
891 for (cur = *lp; cur->next; cur = cur->next)
897 token = next_token(&val, cfile);
898 if (token != STRING) {
899 parse_warn("Expecting media options.");
904 valsize = strlen(val) + 1;
905 tmp = new_string_list(valsize);
907 error("no memory for string list entry.");
908 memcpy(tmp->string, val, valsize);
911 /* Store this medium at the end of the media list. */
918 token = next_token(&val, cfile);
919 } while (multiple && token == COMMA);
922 parse_warn("expecting semicolon.");
928 parse_reject_statement(FILE *cfile, struct client_config *config)
933 struct iaddrlist *list;
936 if (!parse_ip_addr(cfile, &addr)) {
937 parse_warn("expecting IP address.");
942 list = malloc(sizeof(struct iaddrlist));
944 error("no memory for reject list!");
947 list->next = config->reject_list;
948 config->reject_list = list;
950 token = next_token(&val, cfile);
951 } while (token == COMMA);
954 parse_warn("expecting semicolon.");