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_ROUTERS;
94 top_level_config.requested_options
95 [top_level_config.requested_option_count++] = DHO_DOMAIN_NAME;
96 top_level_config.requested_options
97 [top_level_config.requested_option_count++] =
98 DHO_DOMAIN_NAME_SERVERS;
99 top_level_config.requested_options
100 [top_level_config.requested_option_count++] = DHO_HOST_NAME;
102 if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
104 token = peek_token(&val, cfile);
107 parse_client_statement(cfile, NULL, &top_level_config);
109 token = next_token(&val, cfile); /* Clear the peek buffer */
114 * Set up state and config structures for clients that don't
115 * have per-interface configuration declarations.
119 ifi->client = malloc(sizeof(struct client_state));
121 error("no memory for client state.");
122 memset(ifi->client, 0, sizeof(*(ifi->client)));
124 if (!ifi->client->config) {
126 config = malloc(sizeof(struct client_config));
128 error("no memory for client config.");
129 memcpy(config, &top_level_config,
130 sizeof(top_level_config));
132 ifi->client->config = config;
135 return (!warnings_occurred);
139 * lease-file :== client-lease-statements EOF
140 * client-lease-statements :== <nil>
141 * | client-lease-statements LEASE client-lease-statement
144 read_client_leases(void)
150 new_parse(path_dhclient_db);
152 /* Open the lease file. If we can't open it, just return -
153 we can safely trust the server to remember our state. */
154 if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
157 token = next_token(&val, cfile);
160 if (token != LEASE) {
161 warning("Corrupt lease file - possible data loss!");
165 parse_client_lease_statement(cfile, 0);
172 * client-declaration :==
174 * DEFAULT option-decl |
175 * SUPERSEDE option-decl |
176 * PREPEND option-decl |
177 * APPEND option-decl |
178 * hardware-declaration |
179 * REQUEST option-list |
180 * REQUIRE option-list |
184 * SELECT_TIMEOUT number |
186 * interface-declaration |
187 * LEASE client-lease-statement |
188 * ALIAS client-lease-statement
191 parse_client_statement(FILE *cfile, struct interface_info *ip,
192 struct client_config *config)
196 struct option *option;
198 switch (next_token(&val, cfile)) {
200 parse_option_decl(cfile, &config->send_options[0]);
203 option = parse_option_decl(cfile, &config->defaults[0]);
205 config->default_actions[option->code] = ACTION_DEFAULT;
208 option = parse_option_decl(cfile, &config->defaults[0]);
210 config->default_actions[option->code] =
214 option = parse_option_decl(cfile, &config->defaults[0]);
216 config->default_actions[option->code] = ACTION_APPEND;
219 option = parse_option_decl(cfile, &config->defaults[0]);
221 config->default_actions[option->code] = ACTION_PREPEND;
224 parse_string_list(cfile, &config->media, 1);
228 parse_hardware_param(cfile, &ip->hw_address);
230 parse_warn("hardware address parameter %s",
231 "not allowed here.");
236 config->requested_option_count =
237 parse_option_list(cfile, config->requested_options);
240 memset(config->required_options, 0,
241 sizeof(config->required_options));
242 parse_option_list(cfile, config->required_options);
245 parse_lease_time(cfile, &config->timeout);
248 parse_lease_time(cfile, &config->retry_interval);
251 parse_lease_time(cfile, &config->select_interval);
254 parse_lease_time(cfile, &config->reboot_timeout);
257 parse_lease_time(cfile, &config->backoff_cutoff);
259 case INITIAL_INTERVAL:
260 parse_lease_time(cfile, &config->initial_interval);
263 config->script_name = parse_string(cfile);
267 parse_warn("nested interface declaration.");
268 parse_interface_declaration(cfile, config);
271 parse_client_lease_statement(cfile, 1);
274 parse_client_lease_statement(cfile, 2);
277 parse_reject_statement(cfile, config);
280 parse_warn("expecting a statement.");
284 token = next_token(&val, cfile);
286 parse_warn("semicolon expected.");
292 parse_X(FILE *cfile, u_int8_t *buf, int max)
298 token = peek_token(&val, cfile);
299 if (token == NUMBER_OR_NAME || token == NUMBER) {
302 token = next_token(&val, cfile);
303 if (token != NUMBER && token != NUMBER_OR_NAME) {
304 parse_warn("expecting hexadecimal constant.");
308 convert_num(&buf[len], val, 16, 8);
310 parse_warn("hexadecimal constant too long.");
314 token = peek_token(&val, cfile);
316 token = next_token(&val, cfile);
317 } while (token == COLON);
319 } else if (token == STRING) {
320 token = next_token(&val, cfile);
323 parse_warn("string constant too long.");
327 memcpy(buf, val, len + 1);
329 parse_warn("expecting string or hexadecimal data");
337 * option-list :== option_name |
338 * option_list COMMA option_name
341 parse_option_list(FILE *cfile, u_int8_t *list)
349 token = next_token(&val, cfile);
350 if (!is_identifier(token)) {
351 parse_warn("expected option name.");
355 for (i = 0; i < 256; i++)
356 if (!strcasecmp(dhcp_options[i].name, val))
360 parse_warn("%s: unexpected option name.", val);
366 parse_warn("%s: too many options.", val);
370 token = next_token(&val, cfile);
371 } while (token == COMMA);
373 parse_warn("expecting semicolon.");
381 * interface-declaration :==
382 * INTERFACE string LBRACE client-declarations RBRACE
385 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
389 struct interface_info *ip;
391 token = next_token(&val, cfile);
392 if (token != STRING) {
393 parse_warn("expecting interface name (in quotes).");
398 ip = interface_or_dummy(val);
401 make_client_state(ip);
403 if (!ip->client->config)
404 make_client_config(ip, outer_config);
406 token = next_token(&val, cfile);
407 if (token != LBRACE) {
408 parse_warn("expecting left brace.");
414 token = peek_token(&val, cfile);
416 parse_warn("unterminated interface declaration.");
421 parse_client_statement(cfile, ip, ip->client->config);
423 token = next_token(&val, cfile);
426 struct interface_info *
427 interface_or_dummy(char *name)
429 struct interface_info *ip;
431 /* Find the interface (if any) that matches the name. */
432 if (!strcmp(ifi->name, name))
435 /* If it's not a real interface, see if it's on the dummy list. */
436 for (ip = dummy_interfaces; ip; ip = ip->next)
437 if (!strcmp(ip->name, name))
441 * If we didn't find an interface, make a dummy interface as a
444 ip = malloc(sizeof(*ip));
446 error("Insufficient memory to record interface %s", name);
447 memset(ip, 0, sizeof(*ip));
448 strlcpy(ip->name, name, IFNAMSIZ);
449 ip->next = dummy_interfaces;
450 dummy_interfaces = ip;
455 make_client_state(struct interface_info *ip)
457 ip->client = malloc(sizeof(*(ip->client)));
459 error("no memory for state on %s", ip->name);
460 memset(ip->client, 0, sizeof(*(ip->client)));
464 make_client_config(struct interface_info *ip, struct client_config *config)
466 ip->client->config = malloc(sizeof(struct client_config));
467 if (!ip->client->config)
468 error("no memory for config for %s", ip->name);
469 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
470 memcpy(ip->client->config, config, sizeof(*config));
474 * client-lease-statement :==
475 * RBRACE client-lease-declarations LBRACE
477 * client-lease-declarations :==
479 * client-lease-declaration |
480 * client-lease-declarations client-lease-declaration
483 parse_client_lease_statement(FILE *cfile, int is_static)
485 struct client_lease *lease, *lp, *pl;
486 struct interface_info *ip;
490 token = next_token(&val, cfile);
491 if (token != LBRACE) {
492 parse_warn("expecting left brace.");
497 lease = malloc(sizeof(struct client_lease));
499 error("no memory for lease.");
500 memset(lease, 0, sizeof(*lease));
501 lease->is_static = is_static;
506 token = peek_token(&val, cfile);
508 parse_warn("unterminated lease declaration.");
513 parse_client_lease_declaration(cfile, lease, &ip);
515 token = next_token(&val, cfile);
517 /* If the lease declaration didn't include an interface
518 * declaration that we recognized, it's of no use to us.
521 free_client_lease(lease);
525 /* Make sure there's a client state structure... */
527 make_client_state(ip);
529 /* If this is an alias lease, it doesn't need to be sorted in. */
530 if (is_static == 2) {
531 ip->client->alias = lease;
536 * The new lease may supersede a lease that's not the active
537 * lease but is still on the lease list, so scan the lease list
538 * looking for a lease with the same address, and if we find it,
542 for (lp = ip->client->leases; lp; lp = lp->next) {
543 if (lp->address.len == lease->address.len &&
544 !memcmp(lp->address.iabuf, lease->address.iabuf,
545 lease->address.len)) {
549 ip->client->leases = lp->next;
550 free_client_lease(lp);
556 * If this is a preloaded lease, just put it on the list of
557 * recorded leases - don't make it the active lease.
560 lease->next = ip->client->leases;
561 ip->client->leases = lease;
566 * The last lease in the lease file on a particular interface is
567 * the active lease for that interface. Of course, we don't
568 * know what the last lease in the file is until we've parsed
569 * the whole file, so at this point, we assume that the lease we
570 * just parsed is the active lease for its interface. If
571 * there's already an active lease for the interface, and this
572 * lease is for the same ip address, then we just toss the old
573 * active lease and replace it with this one. If this lease is
574 * for a different address, then if the old active lease has
575 * expired, we dump it; if not, we put it on the list of leases
576 * for this interface which are still valid but no longer
579 if (ip->client->active) {
580 if (ip->client->active->expiry < cur_time)
581 free_client_lease(ip->client->active);
582 else if (ip->client->active->address.len ==
583 lease->address.len &&
584 !memcmp(ip->client->active->address.iabuf,
585 lease->address.iabuf, lease->address.len))
586 free_client_lease(ip->client->active);
588 ip->client->active->next = ip->client->leases;
589 ip->client->leases = ip->client->active;
592 ip->client->active = lease;
598 * client-lease-declaration :==
601 * FIXED_ADDR ip_address |
603 * SERVER_NAME string |
604 * OPTION option-decl |
610 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
611 struct interface_info **ipp)
615 struct interface_info *ip;
617 switch (next_token(&val, cfile)) {
622 token = next_token(&val, cfile);
623 if (token != STRING) {
624 parse_warn("expecting interface name (in quotes).");
628 ip = interface_or_dummy(val);
632 if (!parse_ip_addr(cfile, &lease->address))
636 parse_string_list(cfile, &lease->medium, 0);
639 lease->filename = parse_string(cfile);
642 lease->server_name = parse_string(cfile);
645 lease->renewal = parse_date(cfile);
648 lease->rebind = parse_date(cfile);
651 lease->expiry = parse_date(cfile);
654 parse_option_decl(cfile, lease->options);
657 parse_warn("expecting lease declaration.");
661 token = next_token(&val, cfile);
663 parse_warn("expecting semicolon.");
669 parse_option_decl(FILE *cfile, struct option_data *options)
674 u_int8_t hunkbuf[1024];
678 struct universe *universe;
679 struct option *option;
680 struct iaddr ip_addr;
685 token = next_token(&val, cfile);
686 if (!is_identifier(token)) {
687 parse_warn("expecting identifier after option keyword.");
692 if ((vendor = strdup(val)) == NULL)
693 error("no memory for vendor information.");
695 token = peek_token(&val, cfile);
697 /* Go ahead and take the DOT token... */
698 token = next_token(&val, cfile);
700 /* The next token should be an identifier... */
701 token = next_token(&val, cfile);
702 if (!is_identifier(token)) {
703 parse_warn("expecting identifier after '.'");
709 /* Look up the option name hash table for the specified
711 universe = ((struct universe *)hash_lookup(&universe_hash,
712 (unsigned char *)vendor, 0));
713 /* If it's not there, we can't parse the rest of the
716 parse_warn("no vendor named %s.", vendor);
721 /* Use the default hash table, which contains all the
722 standard dhcp option names. */
724 universe = &dhcp_universe;
727 /* Look up the actual option info... */
728 option = (struct option *)hash_lookup(universe->hash,
729 (unsigned char *)val, 0);
731 /* If we didn't get an option structure, it's an undefined option. */
734 parse_warn("no option named %s", val);
736 parse_warn("no option named %s for vendor %s",
742 /* Free the initial identifier token. */
745 /* Parse the option data... */
747 for (fmt = option->format; *fmt; fmt++) {
752 len = parse_X(cfile, &hunkbuf[hunkix],
753 sizeof(hunkbuf) - hunkix);
756 case 't': /* Text string... */
757 token = next_token(&val, cfile);
758 if (token != STRING) {
759 parse_warn("expecting string.");
764 if (hunkix + len + 1 > sizeof(hunkbuf)) {
765 parse_warn("option data buffer %s",
770 memcpy(&hunkbuf[hunkix], val, len + 1);
774 case 'I': /* IP address. */
775 if (!parse_ip_addr(cfile, &ip_addr))
780 if (hunkix + len > sizeof(hunkbuf)) {
781 parse_warn("option data buffer "
786 memcpy(&hunkbuf[hunkix], dp, len);
789 case 'L': /* Unsigned 32-bit integer... */
790 case 'l': /* Signed 32-bit integer... */
791 token = next_token(&val, cfile);
792 if (token != NUMBER) {
794 parse_warn("expecting number.");
799 convert_num(buf, val, 0, 32);
803 case 's': /* Signed 16-bit integer. */
804 case 'S': /* Unsigned 16-bit integer. */
805 token = next_token(&val, cfile);
808 convert_num(buf, val, 0, 16);
812 case 'b': /* Signed 8-bit integer. */
813 case 'B': /* Unsigned 8-bit integer. */
814 token = next_token(&val, cfile);
817 convert_num(buf, val, 0, 8);
821 case 'f': /* Boolean flag. */
822 token = next_token(&val, cfile);
823 if (!is_identifier(token)) {
824 parse_warn("expecting identifier.");
830 if (!strcasecmp(val, "true") ||
831 !strcasecmp(val, "on"))
833 else if (!strcasecmp(val, "false") ||
834 !strcasecmp(val, "off"))
837 parse_warn("expecting boolean.");
844 warning("Bad format %c in parse_option_param.",
850 token = next_token(&val, cfile);
851 } while (*fmt == 'A' && token == COMMA);
854 parse_warn("semicolon expected.");
859 options[option->code].data = malloc(hunkix + nul_term);
860 if (!options[option->code].data)
861 error("out of memory allocating option data.");
862 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
863 options[option->code].len = hunkix;
868 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
872 struct string_list *cur, *tmp;
874 /* Find the last medium in the media list. */
876 for (cur = *lp; cur->next; cur = cur->next)
882 token = next_token(&val, cfile);
883 if (token != STRING) {
884 parse_warn("Expecting media options.");
889 tmp = new_string_list(strlen(val) + 1);
891 error("no memory for string list entry.");
892 strlcpy(tmp->string, val, strlen(val) + 1);
895 /* Store this medium at the end of the media list. */
902 token = next_token(&val, cfile);
903 } while (multiple && token == COMMA);
906 parse_warn("expecting semicolon.");
912 parse_reject_statement(FILE *cfile, struct client_config *config)
917 struct iaddrlist *list;
920 if (!parse_ip_addr(cfile, &addr)) {
921 parse_warn("expecting IP address.");
926 list = malloc(sizeof(struct iaddrlist));
928 error("no memory for reject list!");
931 list->next = config->reject_list;
932 config->reject_list = list;
934 token = next_token(&val, cfile);
935 } while (token == COMMA);
938 parse_warn("expecting semicolon.");