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>
46 __FBSDID("$FreeBSD$");
51 struct client_config top_level_config;
52 static struct interface_info *dummy_interfaces;
54 static char client_script_name[] = "/sbin/dhclient-script";
57 * client-conf-file :== client-declarations EOF
58 * client-declarations :== <nil>
59 * | client-declaration
60 * | client-declarations client-declaration
63 read_client_conf(void)
68 struct client_config *config;
70 new_parse(path_dhclient_conf);
72 /* Set up the initial dhcp option universe. */
73 initialize_universes();
75 /* Initialize the top level client configuration. */
76 memset(&top_level_config, 0, sizeof(top_level_config));
78 /* Set some defaults... */
79 top_level_config.vlan_pcp = 0;
80 top_level_config.timeout = 60;
81 top_level_config.select_interval = 0;
82 top_level_config.reboot_timeout = 10;
83 top_level_config.retry_interval = 300;
84 top_level_config.backoff_cutoff = 15;
85 top_level_config.initial_interval = 3;
86 top_level_config.bootp_policy = ACCEPT;
87 top_level_config.script_name = client_script_name;
88 top_level_config.requested_options
89 [top_level_config.requested_option_count++] = DHO_SUBNET_MASK;
90 top_level_config.requested_options
91 [top_level_config.requested_option_count++] = DHO_BROADCAST_ADDRESS;
92 top_level_config.requested_options
93 [top_level_config.requested_option_count++] = DHO_TIME_OFFSET;
94 top_level_config.requested_options
95 [top_level_config.requested_option_count++] = DHO_CLASSLESS_ROUTES;
96 top_level_config.requested_options
97 [top_level_config.requested_option_count++] = DHO_ROUTERS;
98 top_level_config.requested_options
99 [top_level_config.requested_option_count++] = DHO_DOMAIN_NAME;
100 top_level_config.requested_options
101 [top_level_config.requested_option_count++] =
102 DHO_DOMAIN_NAME_SERVERS;
103 top_level_config.requested_options
104 [top_level_config.requested_option_count++] = DHO_HOST_NAME;
105 top_level_config.requested_options
106 [top_level_config.requested_option_count++] = DHO_DOMAIN_SEARCH;
107 top_level_config.requested_options
108 [top_level_config.requested_option_count++] = DHO_INTERFACE_MTU;
110 if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
112 token = peek_token(&val, cfile);
115 parse_client_statement(cfile, NULL, &top_level_config);
117 token = next_token(&val, cfile); /* Clear the peek buffer */
122 * Set up state and config structures for clients that don't
123 * have per-interface configuration declarations.
127 ifi->client = malloc(sizeof(struct client_state));
129 error("no memory for client state.");
130 memset(ifi->client, 0, sizeof(*(ifi->client)));
132 if (!ifi->client->config) {
134 config = malloc(sizeof(struct client_config));
136 error("no memory for client config.");
137 memcpy(config, &top_level_config,
138 sizeof(top_level_config));
140 ifi->client->config = config;
143 return (!warnings_occurred);
147 * lease-file :== client-lease-statements EOF
148 * client-lease-statements :== <nil>
149 * | client-lease-statements LEASE client-lease-statement
152 read_client_leases(void)
158 new_parse(path_dhclient_db);
160 /* Open the lease file. If we can't open it, just return -
161 we can safely trust the server to remember our state. */
162 if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
165 token = next_token(&val, cfile);
168 if (token != LEASE) {
169 warning("Corrupt lease file - possible data loss!");
173 parse_client_lease_statement(cfile, 0);
180 * client-declaration :==
182 * DEFAULT option-decl |
183 * SUPERSEDE option-decl |
184 * PREPEND option-decl |
185 * APPEND option-decl |
186 * hardware-declaration |
187 * REQUEST option-list |
188 * REQUIRE option-list |
189 * IGNORE option-list |
193 * SELECT_TIMEOUT number |
195 * interface-declaration |
196 * LEASE client-lease-statement |
197 * ALIAS client-lease-statement
200 parse_client_statement(FILE *cfile, struct interface_info *ip,
201 struct client_config *config)
204 struct option *option;
207 switch (next_token(&val, cfile)) {
209 parse_option_decl(cfile, &config->send_options[0]);
212 option = parse_option_decl(cfile, &config->defaults[0]);
214 config->default_actions[option->code] = ACTION_DEFAULT;
217 option = parse_option_decl(cfile, &config->defaults[0]);
219 config->default_actions[option->code] =
223 option = parse_option_decl(cfile, &config->defaults[0]);
225 config->default_actions[option->code] = ACTION_APPEND;
228 option = parse_option_decl(cfile, &config->defaults[0]);
230 config->default_actions[option->code] = ACTION_PREPEND;
233 parse_string_list(cfile, &config->media, 1);
237 parse_hardware_param(cfile, &ip->hw_address);
239 parse_warn("hardware address parameter %s",
240 "not allowed here.");
245 config->requested_option_count =
246 parse_option_list(cfile, config->requested_options);
249 memset(config->required_options, 0,
250 sizeof(config->required_options));
251 parse_option_list(cfile, config->required_options);
254 parse_option_list(cfile, config->ignored_options);
257 parse_lease_time(cfile, &config->timeout);
260 parse_lease_time(cfile, &config->retry_interval);
263 parse_lease_time(cfile, &config->select_interval);
266 parse_lease_time(cfile, &config->reboot_timeout);
269 parse_lease_time(cfile, &tmp);
270 config->vlan_pcp = (u_int)tmp;
273 parse_lease_time(cfile, &config->backoff_cutoff);
275 case INITIAL_INTERVAL:
276 parse_lease_time(cfile, &config->initial_interval);
279 config->script_name = parse_string(cfile);
283 parse_warn("nested interface declaration.");
284 parse_interface_declaration(cfile, config);
287 parse_client_lease_statement(cfile, 1);
290 parse_client_lease_statement(cfile, 2);
293 parse_reject_statement(cfile, config);
299 parse_warn("expecting a statement.");
304 parse_X(FILE *cfile, u_int8_t *buf, unsigned max)
310 token = peek_token(&val, cfile);
311 if (token == NUMBER_OR_NAME || token == NUMBER) {
314 token = next_token(&val, cfile);
315 if (token != NUMBER && token != NUMBER_OR_NAME) {
316 parse_warn("expecting hexadecimal constant.");
320 convert_num(&buf[len], val, 16, 8);
322 parse_warn("hexadecimal constant too long.");
326 token = peek_token(&val, cfile);
328 token = next_token(&val, cfile);
329 } while (token == COLON);
331 } else if (token == STRING) {
332 token = next_token(&val, cfile);
335 parse_warn("string constant too long.");
339 memcpy(buf, val, len + 1);
341 parse_warn("expecting string or hexadecimal data");
349 * option-list :== option_name |
350 * option_list COMMA option_name
353 parse_option_list(FILE *cfile, u_int8_t *list)
361 token = next_token(&val, cfile);
362 if (!is_identifier(token)) {
363 parse_warn("expected option name.");
367 for (i = 0; i < 256; i++)
368 if (!strcasecmp(dhcp_options[i].name, val))
372 parse_warn("%s: unexpected option name.", val);
378 parse_warn("%s: too many options.", val);
382 token = next_token(&val, cfile);
383 } while (token == COMMA);
385 parse_warn("expecting semicolon.");
393 * interface-declaration :==
394 * INTERFACE string LBRACE client-declarations RBRACE
397 parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
401 struct interface_info *ip;
403 token = next_token(&val, cfile);
404 if (token != STRING) {
405 parse_warn("expecting interface name (in quotes).");
410 ip = interface_or_dummy(val);
413 make_client_state(ip);
415 if (!ip->client->config)
416 make_client_config(ip, outer_config);
418 token = next_token(&val, cfile);
419 if (token != LBRACE) {
420 parse_warn("expecting left brace.");
426 token = peek_token(&val, cfile);
428 parse_warn("unterminated interface declaration.");
433 parse_client_statement(cfile, ip, ip->client->config);
435 token = next_token(&val, cfile);
438 struct interface_info *
439 interface_or_dummy(char *name)
441 struct interface_info *ip;
443 /* Find the interface (if any) that matches the name. */
444 if (!strcmp(ifi->name, name))
447 /* If it's not a real interface, see if it's on the dummy list. */
448 for (ip = dummy_interfaces; ip; ip = ip->next)
449 if (!strcmp(ip->name, name))
453 * If we didn't find an interface, make a dummy interface as a
456 ip = malloc(sizeof(*ip));
458 error("Insufficient memory to record interface %s", name);
459 memset(ip, 0, sizeof(*ip));
460 strlcpy(ip->name, name, IFNAMSIZ);
461 ip->next = dummy_interfaces;
462 dummy_interfaces = ip;
467 make_client_state(struct interface_info *ip)
469 ip->client = malloc(sizeof(*(ip->client)));
471 error("no memory for state on %s", ip->name);
472 memset(ip->client, 0, sizeof(*(ip->client)));
476 make_client_config(struct interface_info *ip, struct client_config *config)
478 ip->client->config = malloc(sizeof(struct client_config));
479 if (!ip->client->config)
480 error("no memory for config for %s", ip->name);
481 memset(ip->client->config, 0, sizeof(*(ip->client->config)));
482 memcpy(ip->client->config, config, sizeof(*config));
486 * client-lease-statement :==
487 * RBRACE client-lease-declarations LBRACE
489 * client-lease-declarations :==
491 * client-lease-declaration |
492 * client-lease-declarations client-lease-declaration
495 parse_client_lease_statement(FILE *cfile, int is_static)
497 struct client_lease *lease, *lp, *pl;
498 struct interface_info *ip;
502 token = next_token(&val, cfile);
503 if (token != LBRACE) {
504 parse_warn("expecting left brace.");
509 lease = malloc(sizeof(struct client_lease));
511 error("no memory for lease.");
512 memset(lease, 0, sizeof(*lease));
513 lease->is_static = is_static;
518 token = peek_token(&val, cfile);
520 parse_warn("unterminated lease declaration.");
521 free_client_lease(lease);
526 parse_client_lease_declaration(cfile, lease, &ip);
528 token = next_token(&val, cfile);
530 /* If the lease declaration didn't include an interface
531 * declaration that we recognized, it's of no use to us.
534 free_client_lease(lease);
538 /* Make sure there's a client state structure... */
540 make_client_state(ip);
542 /* If this is an alias lease, it doesn't need to be sorted in. */
543 if (is_static == 2) {
544 ip->client->alias = lease;
549 * The new lease may supersede a lease that's not the active
550 * lease but is still on the lease list, so scan the lease list
551 * looking for a lease with the same address, and if we find it,
555 for (lp = ip->client->leases; lp; lp = lp->next) {
556 if (lp->address.len == lease->address.len &&
557 !memcmp(lp->address.iabuf, lease->address.iabuf,
558 lease->address.len)) {
562 ip->client->leases = lp->next;
563 free_client_lease(lp);
569 * If this is a preloaded lease, just put it on the list of
570 * recorded leases - don't make it the active lease.
573 lease->next = ip->client->leases;
574 ip->client->leases = lease;
579 * The last lease in the lease file on a particular interface is
580 * the active lease for that interface. Of course, we don't
581 * know what the last lease in the file is until we've parsed
582 * the whole file, so at this point, we assume that the lease we
583 * just parsed is the active lease for its interface. If
584 * there's already an active lease for the interface, and this
585 * lease is for the same ip address, then we just toss the old
586 * active lease and replace it with this one. If this lease is
587 * for a different address, then if the old active lease has
588 * expired, we dump it; if not, we put it on the list of leases
589 * for this interface which are still valid but no longer
592 if (ip->client->active) {
593 if (ip->client->active->expiry < cur_time)
594 free_client_lease(ip->client->active);
595 else if (ip->client->active->address.len ==
596 lease->address.len &&
597 !memcmp(ip->client->active->address.iabuf,
598 lease->address.iabuf, lease->address.len))
599 free_client_lease(ip->client->active);
601 ip->client->active->next = ip->client->leases;
602 ip->client->leases = ip->client->active;
605 ip->client->active = lease;
611 * client-lease-declaration :==
614 * FIXED_ADDR ip_address |
616 * SERVER_NAME string |
617 * OPTION option-decl |
623 parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
624 struct interface_info **ipp)
628 struct interface_info *ip;
630 switch (next_token(&val, cfile)) {
635 token = next_token(&val, cfile);
636 if (token != STRING) {
637 parse_warn("expecting interface name (in quotes).");
641 ip = interface_or_dummy(val);
645 if (!parse_ip_addr(cfile, &lease->address))
649 parse_string_list(cfile, &lease->medium, 0);
652 lease->filename = parse_string(cfile);
655 if (!parse_ip_addr(cfile, &lease->nextserver))
659 lease->server_name = parse_string(cfile);
662 lease->renewal = parse_date(cfile);
665 lease->rebind = parse_date(cfile);
668 lease->expiry = parse_date(cfile);
671 parse_option_decl(cfile, lease->options);
674 parse_warn("expecting lease declaration.");
678 token = next_token(&val, cfile);
680 parse_warn("expecting semicolon.");
686 parse_option_decl(FILE *cfile, struct option_data *options)
691 u_int8_t hunkbuf[1024];
695 struct universe *universe;
696 struct option *option;
697 struct iaddr ip_addr;
702 token = next_token(&val, cfile);
703 if (!is_identifier(token)) {
704 parse_warn("expecting identifier after option keyword.");
709 if ((vendor = strdup(val)) == NULL)
710 error("no memory for vendor information.");
712 token = peek_token(&val, cfile);
714 /* Go ahead and take the DOT token... */
715 token = next_token(&val, cfile);
717 /* The next token should be an identifier... */
718 token = next_token(&val, cfile);
719 if (!is_identifier(token)) {
720 parse_warn("expecting identifier after '.'");
727 /* Look up the option name hash table for the specified
729 universe = ((struct universe *)hash_lookup(&universe_hash,
730 (unsigned char *)vendor, 0));
731 /* If it's not there, we can't parse the rest of the
734 parse_warn("no vendor named %s.", vendor);
740 /* Use the default hash table, which contains all the
741 standard dhcp option names. */
743 universe = &dhcp_universe;
746 /* Look up the actual option info... */
747 option = (struct option *)hash_lookup(universe->hash,
748 (unsigned char *)val, 0);
750 /* If we didn't get an option structure, it's an undefined option. */
753 parse_warn("no option named %s", val);
755 parse_warn("no option named %s for vendor %s",
762 /* Free the initial identifier token. */
765 /* Parse the option data... */
767 for (fmt = option->format; *fmt; fmt++) {
772 len = parse_X(cfile, &hunkbuf[hunkix],
773 sizeof(hunkbuf) - hunkix);
776 case 't': /* Text string... */
777 token = next_token(&val, cfile);
778 if (token != STRING) {
779 parse_warn("expecting string.");
784 if (hunkix + len + 1 > sizeof(hunkbuf)) {
785 parse_warn("option data buffer %s",
790 memcpy(&hunkbuf[hunkix], val, len + 1);
794 case 'I': /* IP address. */
795 if (!parse_ip_addr(cfile, &ip_addr))
800 if (hunkix + len > sizeof(hunkbuf)) {
801 parse_warn("option data buffer "
806 memcpy(&hunkbuf[hunkix], dp, len);
809 case 'L': /* Unsigned 32-bit integer... */
810 case 'l': /* Signed 32-bit integer... */
811 token = next_token(&val, cfile);
812 if (token != NUMBER) {
814 parse_warn("expecting number.");
819 convert_num(buf, val, 0, 32);
823 case 's': /* Signed 16-bit integer. */
824 case 'S': /* Unsigned 16-bit integer. */
825 token = next_token(&val, cfile);
828 convert_num(buf, val, 0, 16);
832 case 'b': /* Signed 8-bit integer. */
833 case 'B': /* Unsigned 8-bit integer. */
834 token = next_token(&val, cfile);
837 convert_num(buf, val, 0, 8);
841 case 'f': /* Boolean flag. */
842 token = next_token(&val, cfile);
843 if (!is_identifier(token)) {
844 parse_warn("expecting identifier.");
850 if (!strcasecmp(val, "true") ||
851 !strcasecmp(val, "on"))
853 else if (!strcasecmp(val, "false") ||
854 !strcasecmp(val, "off"))
857 parse_warn("expecting boolean.");
864 warning("Bad format %c in parse_option_param.",
870 token = next_token(&val, cfile);
871 } while (*fmt == 'A' && token == COMMA);
874 parse_warn("semicolon expected.");
879 options[option->code].data = malloc(hunkix + nul_term);
880 if (!options[option->code].data)
881 error("out of memory allocating option data.");
882 memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
883 options[option->code].len = hunkix;
888 parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
893 struct string_list *cur, *tmp;
895 /* Find the last medium in the media list. */
897 for (cur = *lp; cur->next; cur = cur->next)
903 token = next_token(&val, cfile);
904 if (token != STRING) {
905 parse_warn("Expecting media options.");
910 valsize = strlen(val) + 1;
911 tmp = new_string_list(valsize);
913 error("no memory for string list entry.");
914 memcpy(tmp->string, val, valsize);
917 /* Store this medium at the end of the media list. */
924 token = next_token(&val, cfile);
925 } while (multiple && token == COMMA);
928 parse_warn("expecting semicolon.");
934 parse_reject_statement(FILE *cfile, struct client_config *config)
939 struct iaddrlist *list;
942 if (!parse_ip_addr(cfile, &addr)) {
943 parse_warn("expecting IP address.");
948 list = malloc(sizeof(struct iaddrlist));
950 error("no memory for reject list!");
953 list->next = config->reject_list;
954 config->reject_list = list;
956 token = next_token(&val, cfile);
957 } while (token == COMMA);
960 parse_warn("expecting semicolon.");