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;
104 if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
106 token = peek_token(&val, cfile);
109 parse_client_statement(cfile, NULL, &top_level_config);
111 token = next_token(&val, cfile); /* Clear the peek buffer */
116 * Set up state and config structures for clients that don't
117 * have per-interface configuration declarations.
121 ifi->client = malloc(sizeof(struct client_state));
123 error("no memory for client state.");
124 memset(ifi->client, 0, sizeof(*(ifi->client)));
126 if (!ifi->client->config) {
128 config = malloc(sizeof(struct client_config));
130 error("no memory for client config.");
131 memcpy(config, &top_level_config,
132 sizeof(top_level_config));
134 ifi->client->config = config;
137 return (!warnings_occurred);
141 * lease-file :== client-lease-statements EOF
142 * client-lease-statements :== <nil>
143 * | client-lease-statements LEASE client-lease-statement
146 read_client_leases(void)
152 new_parse(path_dhclient_db);
154 /* Open the lease file. If we can't open it, just return -
155 we can safely trust the server to remember our state. */
156 if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
159 token = next_token(&val, cfile);
162 if (token != LEASE) {
163 warning("Corrupt lease file - possible data loss!");
167 parse_client_lease_statement(cfile, 0);
174 * client-declaration :==
176 * DEFAULT option-decl |
177 * SUPERSEDE option-decl |
178 * PREPEND option-decl |
179 * APPEND option-decl |
180 * hardware-declaration |
181 * REQUEST option-list |
182 * REQUIRE option-list |
186 * SELECT_TIMEOUT number |
188 * interface-declaration |
189 * LEASE client-lease-statement |
190 * ALIAS client-lease-statement
193 parse_client_statement(FILE *cfile, struct interface_info *ip,
194 struct client_config *config)
198 struct option *option;
200 switch (next_token(&val, cfile)) {
202 parse_option_decl(cfile, &config->send_options[0]);
205 option = parse_option_decl(cfile, &config->defaults[0]);
207 config->default_actions[option->code] = ACTION_DEFAULT;
210 option = parse_option_decl(cfile, &config->defaults[0]);
212 config->default_actions[option->code] =
216 option = parse_option_decl(cfile, &config->defaults[0]);
218 config->default_actions[option->code] = ACTION_APPEND;
221 option = parse_option_decl(cfile, &config->defaults[0]);
223 config->default_actions[option->code] = ACTION_PREPEND;
226 parse_string_list(cfile, &config->media, 1);
230 parse_hardware_param(cfile, &ip->hw_address);
232 parse_warn("hardware address parameter %s",
233 "not allowed here.");
238 config->requested_option_count =
239 parse_option_list(cfile, config->requested_options);
242 memset(config->required_options, 0,
243 sizeof(config->required_options));
244 parse_option_list(cfile, config->required_options);
247 parse_lease_time(cfile, &config->timeout);
250 parse_lease_time(cfile, &config->retry_interval);
253 parse_lease_time(cfile, &config->select_interval);
256 parse_lease_time(cfile, &config->reboot_timeout);
259 parse_lease_time(cfile, &config->backoff_cutoff);
261 case INITIAL_INTERVAL:
262 parse_lease_time(cfile, &config->initial_interval);
265 config->script_name = parse_string(cfile);
269 parse_warn("nested interface declaration.");
270 parse_interface_declaration(cfile, config);
273 parse_client_lease_statement(cfile, 1);
276 parse_client_lease_statement(cfile, 2);
279 parse_reject_statement(cfile, config);
282 parse_warn("expecting a statement.");
286 token = next_token(&val, cfile);
288 parse_warn("semicolon expected.");
294 parse_X(FILE *cfile, u_int8_t *buf, int max)
300 token = peek_token(&val, cfile);
301 if (token == NUMBER_OR_NAME || token == NUMBER) {
304 token = next_token(&val, cfile);
305 if (token != NUMBER && token != NUMBER_OR_NAME) {
306 parse_warn("expecting hexadecimal constant.");
310 convert_num(&buf[len], val, 16, 8);
312 parse_warn("hexadecimal constant too long.");
316 token = peek_token(&val, cfile);
318 token = next_token(&val, cfile);
319 } while (token == COLON);
321 } else if (token == STRING) {
322 token = next_token(&val, cfile);
325 parse_warn("string constant too long.");
329 memcpy(buf, val, len + 1);
331 parse_warn("expecting string or hexadecimal data");
339 * option-list :== option_name |
340 * option_list COMMA option_name
343 parse_option_list(FILE *cfile, u_int8_t *list)
351 token = next_token(&val, cfile);
352 if (!is_identifier(token)) {
353 parse_warn("expected option name.");
357 for (i = 0; i < 256; i++)
358 if (!strcasecmp(dhcp_options[i].name, val))
362 parse_warn("%s: unexpected option name.", val);
368 parse_warn("%s: too many options.", val);
372 token = next_token(&val, cfile);
373 } while (token == COMMA);
375 parse_warn("expecting semicolon.");
383 * interface-declaration :==
384 * INTERFACE string LBRACE client-declarations RBRACE
387 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
391 struct interface_info *ip;
393 token = next_token(&val, cfile);
394 if (token != STRING) {
395 parse_warn("expecting interface name (in quotes).");
400 ip = interface_or_dummy(val);
403 make_client_state(ip);
405 if (!ip->client->config)
406 make_client_config(ip, outer_config);
408 token = next_token(&val, cfile);
409 if (token != LBRACE) {
410 parse_warn("expecting left brace.");
416 token = peek_token(&val, cfile);
418 parse_warn("unterminated interface declaration.");
423 parse_client_statement(cfile, ip, ip->client->config);
425 token = next_token(&val, cfile);
428 struct interface_info *
429 interface_or_dummy(char *name)
431 struct interface_info *ip;
433 /* Find the interface (if any) that matches the name. */
434 if (!strcmp(ifi->name, name))
437 /* If it's not a real interface, see if it's on the dummy list. */
438 for (ip = dummy_interfaces; ip; ip = ip->next)
439 if (!strcmp(ip->name, name))
443 * If we didn't find an interface, make a dummy interface as a
446 ip = malloc(sizeof(*ip));
448 error("Insufficient memory to record interface %s", name);
449 memset(ip, 0, sizeof(*ip));
450 strlcpy(ip->name, name, IFNAMSIZ);
451 ip->next = dummy_interfaces;
452 dummy_interfaces = ip;
457 make_client_state(struct interface_info *ip)
459 ip->client = malloc(sizeof(*(ip->client)));
461 error("no memory for state on %s", ip->name);
462 memset(ip->client, 0, sizeof(*(ip->client)));
466 make_client_config(struct interface_info *ip, struct client_config *config)
468 ip->client->config = malloc(sizeof(struct client_config));
469 if (!ip->client->config)
470 error("no memory for config for %s", ip->name);
471 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
472 memcpy(ip->client->config, config, sizeof(*config));
476 * client-lease-statement :==
477 * RBRACE client-lease-declarations LBRACE
479 * client-lease-declarations :==
481 * client-lease-declaration |
482 * client-lease-declarations client-lease-declaration
485 parse_client_lease_statement(FILE *cfile, int is_static)
487 struct client_lease *lease, *lp, *pl;
488 struct interface_info *ip;
492 token = next_token(&val, cfile);
493 if (token != LBRACE) {
494 parse_warn("expecting left brace.");
499 lease = malloc(sizeof(struct client_lease));
501 error("no memory for lease.");
502 memset(lease, 0, sizeof(*lease));
503 lease->is_static = is_static;
508 token = peek_token(&val, cfile);
510 parse_warn("unterminated lease declaration.");
515 parse_client_lease_declaration(cfile, lease, &ip);
517 token = next_token(&val, cfile);
519 /* If the lease declaration didn't include an interface
520 * declaration that we recognized, it's of no use to us.
523 free_client_lease(lease);
527 /* Make sure there's a client state structure... */
529 make_client_state(ip);
531 /* If this is an alias lease, it doesn't need to be sorted in. */
532 if (is_static == 2) {
533 ip->client->alias = lease;
538 * The new lease may supersede a lease that's not the active
539 * lease but is still on the lease list, so scan the lease list
540 * looking for a lease with the same address, and if we find it,
544 for (lp = ip->client->leases; lp; lp = lp->next) {
545 if (lp->address.len == lease->address.len &&
546 !memcmp(lp->address.iabuf, lease->address.iabuf,
547 lease->address.len)) {
551 ip->client->leases = lp->next;
552 free_client_lease(lp);
558 * If this is a preloaded lease, just put it on the list of
559 * recorded leases - don't make it the active lease.
562 lease->next = ip->client->leases;
563 ip->client->leases = lease;
568 * The last lease in the lease file on a particular interface is
569 * the active lease for that interface. Of course, we don't
570 * know what the last lease in the file is until we've parsed
571 * the whole file, so at this point, we assume that the lease we
572 * just parsed is the active lease for its interface. If
573 * there's already an active lease for the interface, and this
574 * lease is for the same ip address, then we just toss the old
575 * active lease and replace it with this one. If this lease is
576 * for a different address, then if the old active lease has
577 * expired, we dump it; if not, we put it on the list of leases
578 * for this interface which are still valid but no longer
581 if (ip->client->active) {
582 if (ip->client->active->expiry < cur_time)
583 free_client_lease(ip->client->active);
584 else if (ip->client->active->address.len ==
585 lease->address.len &&
586 !memcmp(ip->client->active->address.iabuf,
587 lease->address.iabuf, lease->address.len))
588 free_client_lease(ip->client->active);
590 ip->client->active->next = ip->client->leases;
591 ip->client->leases = ip->client->active;
594 ip->client->active = lease;
600 * client-lease-declaration :==
603 * FIXED_ADDR ip_address |
605 * SERVER_NAME string |
606 * OPTION option-decl |
612 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
613 struct interface_info **ipp)
617 struct interface_info *ip;
619 switch (next_token(&val, cfile)) {
624 token = next_token(&val, cfile);
625 if (token != STRING) {
626 parse_warn("expecting interface name (in quotes).");
630 ip = interface_or_dummy(val);
634 if (!parse_ip_addr(cfile, &lease->address))
638 parse_string_list(cfile, &lease->medium, 0);
641 lease->filename = parse_string(cfile);
644 lease->server_name = parse_string(cfile);
647 lease->renewal = parse_date(cfile);
650 lease->rebind = parse_date(cfile);
653 lease->expiry = parse_date(cfile);
656 parse_option_decl(cfile, lease->options);
659 parse_warn("expecting lease declaration.");
663 token = next_token(&val, cfile);
665 parse_warn("expecting semicolon.");
671 parse_option_decl(FILE *cfile, struct option_data *options)
676 u_int8_t hunkbuf[1024];
680 struct universe *universe;
681 struct option *option;
682 struct iaddr ip_addr;
687 token = next_token(&val, cfile);
688 if (!is_identifier(token)) {
689 parse_warn("expecting identifier after option keyword.");
694 if ((vendor = strdup(val)) == NULL)
695 error("no memory for vendor information.");
697 token = peek_token(&val, cfile);
699 /* Go ahead and take the DOT token... */
700 token = next_token(&val, cfile);
702 /* The next token should be an identifier... */
703 token = next_token(&val, cfile);
704 if (!is_identifier(token)) {
705 parse_warn("expecting identifier after '.'");
711 /* Look up the option name hash table for the specified
713 universe = ((struct universe *)hash_lookup(&universe_hash,
714 (unsigned char *)vendor, 0));
715 /* If it's not there, we can't parse the rest of the
718 parse_warn("no vendor named %s.", vendor);
723 /* Use the default hash table, which contains all the
724 standard dhcp option names. */
726 universe = &dhcp_universe;
729 /* Look up the actual option info... */
730 option = (struct option *)hash_lookup(universe->hash,
731 (unsigned char *)val, 0);
733 /* If we didn't get an option structure, it's an undefined option. */
736 parse_warn("no option named %s", val);
738 parse_warn("no option named %s for vendor %s",
744 /* Free the initial identifier token. */
747 /* Parse the option data... */
749 for (fmt = option->format; *fmt; fmt++) {
754 len = parse_X(cfile, &hunkbuf[hunkix],
755 sizeof(hunkbuf) - hunkix);
758 case 't': /* Text string... */
759 token = next_token(&val, cfile);
760 if (token != STRING) {
761 parse_warn("expecting string.");
766 if (hunkix + len + 1 > sizeof(hunkbuf)) {
767 parse_warn("option data buffer %s",
772 memcpy(&hunkbuf[hunkix], val, len + 1);
776 case 'I': /* IP address. */
777 if (!parse_ip_addr(cfile, &ip_addr))
782 if (hunkix + len > sizeof(hunkbuf)) {
783 parse_warn("option data buffer "
788 memcpy(&hunkbuf[hunkix], dp, len);
791 case 'L': /* Unsigned 32-bit integer... */
792 case 'l': /* Signed 32-bit integer... */
793 token = next_token(&val, cfile);
794 if (token != NUMBER) {
796 parse_warn("expecting number.");
801 convert_num(buf, val, 0, 32);
805 case 's': /* Signed 16-bit integer. */
806 case 'S': /* Unsigned 16-bit integer. */
807 token = next_token(&val, cfile);
810 convert_num(buf, val, 0, 16);
814 case 'b': /* Signed 8-bit integer. */
815 case 'B': /* Unsigned 8-bit integer. */
816 token = next_token(&val, cfile);
819 convert_num(buf, val, 0, 8);
823 case 'f': /* Boolean flag. */
824 token = next_token(&val, cfile);
825 if (!is_identifier(token)) {
826 parse_warn("expecting identifier.");
832 if (!strcasecmp(val, "true") ||
833 !strcasecmp(val, "on"))
835 else if (!strcasecmp(val, "false") ||
836 !strcasecmp(val, "off"))
839 parse_warn("expecting boolean.");
846 warning("Bad format %c in parse_option_param.",
852 token = next_token(&val, cfile);
853 } while (*fmt == 'A' && token == COMMA);
856 parse_warn("semicolon expected.");
861 options[option->code].data = malloc(hunkix + nul_term);
862 if (!options[option->code].data)
863 error("out of memory allocating option data.");
864 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
865 options[option->code].len = hunkix;
870 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
874 struct string_list *cur, *tmp;
876 /* Find the last medium in the media list. */
878 for (cur = *lp; cur->next; cur = cur->next)
884 token = next_token(&val, cfile);
885 if (token != STRING) {
886 parse_warn("Expecting media options.");
891 tmp = new_string_list(strlen(val) + 1);
893 error("no memory for string list entry.");
894 strlcpy(tmp->string, val, strlen(val) + 1);
897 /* Store this medium at the end of the media list. */
904 token = next_token(&val, cfile);
905 } while (multiple && token == COMMA);
908 parse_warn("expecting semicolon.");
914 parse_reject_statement(FILE *cfile, struct client_config *config)
919 struct iaddrlist *list;
922 if (!parse_ip_addr(cfile, &addr)) {
923 parse_warn("expecting IP address.");
928 list = malloc(sizeof(struct iaddrlist));
930 error("no memory for reject list!");
933 list->next = config->reject_list;
934 config->reject_list = list;
936 token = next_token(&val, cfile);
937 } while (token == COMMA);
940 parse_warn("expecting semicolon.");