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;
106 if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
108 token = peek_token(&val, cfile);
111 parse_client_statement(cfile, NULL, &top_level_config);
113 token = next_token(&val, cfile); /* Clear the peek buffer */
118 * Set up state and config structures for clients that don't
119 * have per-interface configuration declarations.
123 ifi->client = malloc(sizeof(struct client_state));
125 error("no memory for client state.");
126 memset(ifi->client, 0, sizeof(*(ifi->client)));
128 if (!ifi->client->config) {
130 config = malloc(sizeof(struct client_config));
132 error("no memory for client config.");
133 memcpy(config, &top_level_config,
134 sizeof(top_level_config));
136 ifi->client->config = config;
139 return (!warnings_occurred);
143 * lease-file :== client-lease-statements EOF
144 * client-lease-statements :== <nil>
145 * | client-lease-statements LEASE client-lease-statement
148 read_client_leases(void)
154 new_parse(path_dhclient_db);
156 /* Open the lease file. If we can't open it, just return -
157 we can safely trust the server to remember our state. */
158 if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
161 token = next_token(&val, cfile);
164 if (token != LEASE) {
165 warning("Corrupt lease file - possible data loss!");
169 parse_client_lease_statement(cfile, 0);
176 * client-declaration :==
178 * DEFAULT option-decl |
179 * SUPERSEDE option-decl |
180 * PREPEND option-decl |
181 * APPEND option-decl |
182 * hardware-declaration |
183 * REQUEST option-list |
184 * REQUIRE option-list |
188 * SELECT_TIMEOUT number |
190 * interface-declaration |
191 * LEASE client-lease-statement |
192 * ALIAS client-lease-statement
195 parse_client_statement(FILE *cfile, struct interface_info *ip,
196 struct client_config *config)
200 struct option *option;
202 switch (next_token(&val, cfile)) {
204 parse_option_decl(cfile, &config->send_options[0]);
207 option = parse_option_decl(cfile, &config->defaults[0]);
209 config->default_actions[option->code] = ACTION_DEFAULT;
212 option = parse_option_decl(cfile, &config->defaults[0]);
214 config->default_actions[option->code] =
218 option = parse_option_decl(cfile, &config->defaults[0]);
220 config->default_actions[option->code] = ACTION_APPEND;
223 option = parse_option_decl(cfile, &config->defaults[0]);
225 config->default_actions[option->code] = ACTION_PREPEND;
228 parse_string_list(cfile, &config->media, 1);
232 parse_hardware_param(cfile, &ip->hw_address);
234 parse_warn("hardware address parameter %s",
235 "not allowed here.");
240 config->requested_option_count =
241 parse_option_list(cfile, config->requested_options);
244 memset(config->required_options, 0,
245 sizeof(config->required_options));
246 parse_option_list(cfile, config->required_options);
249 parse_lease_time(cfile, &config->timeout);
252 parse_lease_time(cfile, &config->retry_interval);
255 parse_lease_time(cfile, &config->select_interval);
258 parse_lease_time(cfile, &config->reboot_timeout);
261 parse_lease_time(cfile, &config->backoff_cutoff);
263 case INITIAL_INTERVAL:
264 parse_lease_time(cfile, &config->initial_interval);
267 config->script_name = parse_string(cfile);
271 parse_warn("nested interface declaration.");
272 parse_interface_declaration(cfile, config);
275 parse_client_lease_statement(cfile, 1);
278 parse_client_lease_statement(cfile, 2);
281 parse_reject_statement(cfile, config);
284 parse_warn("expecting a statement.");
288 token = next_token(&val, cfile);
290 parse_warn("semicolon expected.");
296 parse_X(FILE *cfile, u_int8_t *buf, int max)
302 token = peek_token(&val, cfile);
303 if (token == NUMBER_OR_NAME || token == NUMBER) {
306 token = next_token(&val, cfile);
307 if (token != NUMBER && token != NUMBER_OR_NAME) {
308 parse_warn("expecting hexadecimal constant.");
312 convert_num(&buf[len], val, 16, 8);
314 parse_warn("hexadecimal constant too long.");
318 token = peek_token(&val, cfile);
320 token = next_token(&val, cfile);
321 } while (token == COLON);
323 } else if (token == STRING) {
324 token = next_token(&val, cfile);
327 parse_warn("string constant too long.");
331 memcpy(buf, val, len + 1);
333 parse_warn("expecting string or hexadecimal data");
341 * option-list :== option_name |
342 * option_list COMMA option_name
345 parse_option_list(FILE *cfile, u_int8_t *list)
353 token = next_token(&val, cfile);
354 if (!is_identifier(token)) {
355 parse_warn("expected option name.");
359 for (i = 0; i < 256; i++)
360 if (!strcasecmp(dhcp_options[i].name, val))
364 parse_warn("%s: unexpected option name.", val);
370 parse_warn("%s: too many options.", val);
374 token = next_token(&val, cfile);
375 } while (token == COMMA);
377 parse_warn("expecting semicolon.");
385 * interface-declaration :==
386 * INTERFACE string LBRACE client-declarations RBRACE
389 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
393 struct interface_info *ip;
395 token = next_token(&val, cfile);
396 if (token != STRING) {
397 parse_warn("expecting interface name (in quotes).");
402 ip = interface_or_dummy(val);
405 make_client_state(ip);
407 if (!ip->client->config)
408 make_client_config(ip, outer_config);
410 token = next_token(&val, cfile);
411 if (token != LBRACE) {
412 parse_warn("expecting left brace.");
418 token = peek_token(&val, cfile);
420 parse_warn("unterminated interface declaration.");
425 parse_client_statement(cfile, ip, ip->client->config);
427 token = next_token(&val, cfile);
430 struct interface_info *
431 interface_or_dummy(char *name)
433 struct interface_info *ip;
435 /* Find the interface (if any) that matches the name. */
436 if (!strcmp(ifi->name, name))
439 /* If it's not a real interface, see if it's on the dummy list. */
440 for (ip = dummy_interfaces; ip; ip = ip->next)
441 if (!strcmp(ip->name, name))
445 * If we didn't find an interface, make a dummy interface as a
448 ip = malloc(sizeof(*ip));
450 error("Insufficient memory to record interface %s", name);
451 memset(ip, 0, sizeof(*ip));
452 strlcpy(ip->name, name, IFNAMSIZ);
453 ip->next = dummy_interfaces;
454 dummy_interfaces = ip;
459 make_client_state(struct interface_info *ip)
461 ip->client = malloc(sizeof(*(ip->client)));
463 error("no memory for state on %s", ip->name);
464 memset(ip->client, 0, sizeof(*(ip->client)));
468 make_client_config(struct interface_info *ip, struct client_config *config)
470 ip->client->config = malloc(sizeof(struct client_config));
471 if (!ip->client->config)
472 error("no memory for config for %s", ip->name);
473 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
474 memcpy(ip->client->config, config, sizeof(*config));
478 * client-lease-statement :==
479 * RBRACE client-lease-declarations LBRACE
481 * client-lease-declarations :==
483 * client-lease-declaration |
484 * client-lease-declarations client-lease-declaration
487 parse_client_lease_statement(FILE *cfile, int is_static)
489 struct client_lease *lease, *lp, *pl;
490 struct interface_info *ip;
494 token = next_token(&val, cfile);
495 if (token != LBRACE) {
496 parse_warn("expecting left brace.");
501 lease = malloc(sizeof(struct client_lease));
503 error("no memory for lease.");
504 memset(lease, 0, sizeof(*lease));
505 lease->is_static = is_static;
510 token = peek_token(&val, cfile);
512 parse_warn("unterminated lease declaration.");
517 parse_client_lease_declaration(cfile, lease, &ip);
519 token = next_token(&val, cfile);
521 /* If the lease declaration didn't include an interface
522 * declaration that we recognized, it's of no use to us.
525 free_client_lease(lease);
529 /* Make sure there's a client state structure... */
531 make_client_state(ip);
533 /* If this is an alias lease, it doesn't need to be sorted in. */
534 if (is_static == 2) {
535 ip->client->alias = lease;
540 * The new lease may supersede a lease that's not the active
541 * lease but is still on the lease list, so scan the lease list
542 * looking for a lease with the same address, and if we find it,
546 for (lp = ip->client->leases; lp; lp = lp->next) {
547 if (lp->address.len == lease->address.len &&
548 !memcmp(lp->address.iabuf, lease->address.iabuf,
549 lease->address.len)) {
553 ip->client->leases = lp->next;
554 free_client_lease(lp);
560 * If this is a preloaded lease, just put it on the list of
561 * recorded leases - don't make it the active lease.
564 lease->next = ip->client->leases;
565 ip->client->leases = lease;
570 * The last lease in the lease file on a particular interface is
571 * the active lease for that interface. Of course, we don't
572 * know what the last lease in the file is until we've parsed
573 * the whole file, so at this point, we assume that the lease we
574 * just parsed is the active lease for its interface. If
575 * there's already an active lease for the interface, and this
576 * lease is for the same ip address, then we just toss the old
577 * active lease and replace it with this one. If this lease is
578 * for a different address, then if the old active lease has
579 * expired, we dump it; if not, we put it on the list of leases
580 * for this interface which are still valid but no longer
583 if (ip->client->active) {
584 if (ip->client->active->expiry < cur_time)
585 free_client_lease(ip->client->active);
586 else if (ip->client->active->address.len ==
587 lease->address.len &&
588 !memcmp(ip->client->active->address.iabuf,
589 lease->address.iabuf, lease->address.len))
590 free_client_lease(ip->client->active);
592 ip->client->active->next = ip->client->leases;
593 ip->client->leases = ip->client->active;
596 ip->client->active = lease;
602 * client-lease-declaration :==
605 * FIXED_ADDR ip_address |
607 * SERVER_NAME string |
608 * OPTION option-decl |
614 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
615 struct interface_info **ipp)
619 struct interface_info *ip;
621 switch (next_token(&val, cfile)) {
626 token = next_token(&val, cfile);
627 if (token != STRING) {
628 parse_warn("expecting interface name (in quotes).");
632 ip = interface_or_dummy(val);
636 if (!parse_ip_addr(cfile, &lease->address))
640 parse_string_list(cfile, &lease->medium, 0);
643 lease->filename = parse_string(cfile);
646 if (!parse_ip_addr(cfile, &lease->nextserver))
650 lease->server_name = parse_string(cfile);
653 lease->renewal = parse_date(cfile);
656 lease->rebind = parse_date(cfile);
659 lease->expiry = parse_date(cfile);
662 parse_option_decl(cfile, lease->options);
665 parse_warn("expecting lease declaration.");
669 token = next_token(&val, cfile);
671 parse_warn("expecting semicolon.");
677 parse_option_decl(FILE *cfile, struct option_data *options)
682 u_int8_t hunkbuf[1024];
686 struct universe *universe;
687 struct option *option;
688 struct iaddr ip_addr;
693 token = next_token(&val, cfile);
694 if (!is_identifier(token)) {
695 parse_warn("expecting identifier after option keyword.");
700 if ((vendor = strdup(val)) == NULL)
701 error("no memory for vendor information.");
703 token = peek_token(&val, cfile);
705 /* Go ahead and take the DOT token... */
706 token = next_token(&val, cfile);
708 /* The next token should be an identifier... */
709 token = next_token(&val, cfile);
710 if (!is_identifier(token)) {
711 parse_warn("expecting identifier after '.'");
717 /* Look up the option name hash table for the specified
719 universe = ((struct universe *)hash_lookup(&universe_hash,
720 (unsigned char *)vendor, 0));
721 /* If it's not there, we can't parse the rest of the
724 parse_warn("no vendor named %s.", vendor);
729 /* Use the default hash table, which contains all the
730 standard dhcp option names. */
732 universe = &dhcp_universe;
735 /* Look up the actual option info... */
736 option = (struct option *)hash_lookup(universe->hash,
737 (unsigned char *)val, 0);
739 /* If we didn't get an option structure, it's an undefined option. */
742 parse_warn("no option named %s", val);
744 parse_warn("no option named %s for vendor %s",
750 /* Free the initial identifier token. */
753 /* Parse the option data... */
755 for (fmt = option->format; *fmt; fmt++) {
760 len = parse_X(cfile, &hunkbuf[hunkix],
761 sizeof(hunkbuf) - hunkix);
764 case 't': /* Text string... */
765 token = next_token(&val, cfile);
766 if (token != STRING) {
767 parse_warn("expecting string.");
772 if (hunkix + len + 1 > sizeof(hunkbuf)) {
773 parse_warn("option data buffer %s",
778 memcpy(&hunkbuf[hunkix], val, len + 1);
782 case 'I': /* IP address. */
783 if (!parse_ip_addr(cfile, &ip_addr))
788 if (hunkix + len > sizeof(hunkbuf)) {
789 parse_warn("option data buffer "
794 memcpy(&hunkbuf[hunkix], dp, len);
797 case 'L': /* Unsigned 32-bit integer... */
798 case 'l': /* Signed 32-bit integer... */
799 token = next_token(&val, cfile);
800 if (token != NUMBER) {
802 parse_warn("expecting number.");
807 convert_num(buf, val, 0, 32);
811 case 's': /* Signed 16-bit integer. */
812 case 'S': /* Unsigned 16-bit integer. */
813 token = next_token(&val, cfile);
816 convert_num(buf, val, 0, 16);
820 case 'b': /* Signed 8-bit integer. */
821 case 'B': /* Unsigned 8-bit integer. */
822 token = next_token(&val, cfile);
825 convert_num(buf, val, 0, 8);
829 case 'f': /* Boolean flag. */
830 token = next_token(&val, cfile);
831 if (!is_identifier(token)) {
832 parse_warn("expecting identifier.");
838 if (!strcasecmp(val, "true") ||
839 !strcasecmp(val, "on"))
841 else if (!strcasecmp(val, "false") ||
842 !strcasecmp(val, "off"))
845 parse_warn("expecting boolean.");
852 warning("Bad format %c in parse_option_param.",
858 token = next_token(&val, cfile);
859 } while (*fmt == 'A' && token == COMMA);
862 parse_warn("semicolon expected.");
867 options[option->code].data = malloc(hunkix + nul_term);
868 if (!options[option->code].data)
869 error("out of memory allocating option data.");
870 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
871 options[option->code].len = hunkix;
876 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
881 struct string_list *cur, *tmp;
883 /* Find the last medium in the media list. */
885 for (cur = *lp; cur->next; cur = cur->next)
891 token = next_token(&val, cfile);
892 if (token != STRING) {
893 parse_warn("Expecting media options.");
898 valsize = strlen(val) + 1;
899 tmp = new_string_list(valsize);
901 error("no memory for string list entry.");
902 memcpy(tmp->string, val, valsize);
905 /* Store this medium at the end of the media list. */
912 token = next_token(&val, cfile);
913 } while (multiple && token == COMMA);
916 parse_warn("expecting semicolon.");
922 parse_reject_statement(FILE *cfile, struct client_config *config)
927 struct iaddrlist *list;
930 if (!parse_ip_addr(cfile, &addr)) {
931 parse_warn("expecting IP address.");
936 list = malloc(sizeof(struct iaddrlist));
938 error("no memory for reject list!");
941 list->next = config->reject_list;
942 config->reject_list = list;
944 token = next_token(&val, cfile);
945 } while (token == COMMA);
948 parse_warn("expecting semicolon.");