2
0
mirror of https://gitlab.isc.org/isc-projects/dhcp synced 2025-08-22 18:07:25 +00:00
isc-dhcp/client/clparse.c

1246 lines
30 KiB
C
Raw Normal View History

1997-02-18 14:27:53 +00:00
/* clparse.c
Parser for dhclient config and lease files... */
/*
* Copyright (c) 1996-2000 Internet Software Consortium.
1999-03-16 05:50:46 +00:00
* Use is subject to license terms which appear in the file named
* ISC-LICENSE that should have accompanied this file when you
* received it. If a file named ISC-LICENSE did not accompany this
* file, or you are not sure the one you have is correct, you may
* obtain an applicable copy of the license at:
1997-02-18 14:27:53 +00:00
*
1999-03-16 05:50:46 +00:00
* http://www.isc.org/isc-license-1.0.html.
1997-02-18 14:27:53 +00:00
*
1999-03-16 05:50:46 +00:00
* This file is part of the ISC DHCP distribution. The documentation
* associated with this file is listed in the file DOCUMENTATION,
* included in the top-level directory of this release.
1997-02-18 14:27:53 +00:00
*
1999-03-16 05:50:46 +00:00
* Support and other services are available for ISC products - see
* http://www.isc.org for more information.
1997-02-18 14:27:53 +00:00
*/
#ifndef lint
static char copyright[] =
"$Id: clparse.c,v 1.41 2000/01/26 14:55:26 mellon Exp $ Copyright (c) 1997 The Internet Software Consortium. All rights reserved.\n";
1997-02-18 14:27:53 +00:00
#endif /* not lint */
#include "dhcpd.h"
static TIME parsed_time;
struct client_config top_level_config;
u_int32_t default_requested_options [] = {
DHO_SUBNET_MASK,
DHO_BROADCAST_ADDRESS,
DHO_TIME_OFFSET,
DHO_ROUTERS,
DHO_DOMAIN_NAME,
DHO_DOMAIN_NAME_SERVERS,
DHO_HOST_NAME,
0
};
1997-02-18 14:27:53 +00:00
/* client-conf-file :== client-declarations EOF
client-declarations :== <nil>
| client-declaration
| client-declarations client-declaration */
isc_result_t read_client_conf ()
1997-02-18 14:27:53 +00:00
{
int file;
struct parse *cfile;
const char *val;
1997-02-18 14:27:53 +00:00
int token;
int declaration = 0;
struct client_config *config;
struct client_state *state;
struct interface_info *ip;
isc_result_t status;
1997-02-18 14:27:53 +00:00
/* Set up the initial dhcp option universe. */
initialize_common_option_spaces ();
1997-02-18 14:27:53 +00:00
/* Initialize the top level client configuration. */
memset (&top_level_config, 0, sizeof top_level_config);
/* Set some defaults... */
top_level_config.timeout = 60;
top_level_config.select_interval = 0;
top_level_config.reboot_timeout = 10;
top_level_config.retry_interval = 300;
1999-03-26 19:19:46 +00:00
top_level_config.backoff_cutoff = 15;
top_level_config.initial_interval = 3;
1999-03-16 06:37:55 +00:00
top_level_config.bootp_policy = P_ACCEPT;
top_level_config.script_name = "/etc/dhclient-script";
top_level_config.requested_options = default_requested_options;
2000-01-25 00:58:02 +00:00
top_level_config.on_receipt = new_group (MDL);
1998-11-11 07:48:23 +00:00
if (!top_level_config.on_receipt)
log_fatal ("no memory for top-level on_receipt group");
1998-11-11 07:48:23 +00:00
2000-01-25 00:58:02 +00:00
top_level_config.on_transmission = new_group (MDL);
1998-11-11 07:48:23 +00:00
if (!top_level_config.on_transmission)
log_fatal ("no memory for top-level on_transmission group");
1998-11-11 07:48:23 +00:00
if ((file = open (path_dhclient_conf, O_RDONLY)) >= 0) {
cfile = (struct parse *)0;
new_parse (&cfile, file, (char *)0, 0, path_dhclient_conf);
do {
token = peek_token (&val, cfile);
if (token == EOF)
break;
parse_client_statement (cfile,
(struct interface_info *)0,
&top_level_config);
} while (1);
token = next_token (&val, cfile); /* Clear the peek buffer */
status = (cfile -> warnings_occurred
? ISC_R_BADPARSE
: ISC_R_SUCCESS);
close (file);
end_parse (&cfile);
}
1997-02-18 14:27:53 +00:00
/* Set up state and config structures for clients that don't
1998-10-22 04:52:23 +00:00
have per-interface configuration statements. */
1997-02-18 14:27:53 +00:00
config = (struct client_config *)0;
for (ip = interfaces; ip; ip = ip -> next) {
if (!ip -> client) {
ip -> client = (struct client_state *)
2000-01-25 00:58:02 +00:00
dmalloc (sizeof (struct client_state), MDL);
1997-02-18 14:27:53 +00:00
if (!ip -> client)
log_fatal ("no memory for client state.");
1997-02-18 14:27:53 +00:00
memset (ip -> client, 0, sizeof *(ip -> client));
ip -> client -> interface = ip;
}
1998-10-22 04:52:23 +00:00
if (!ip -> client -> config) {
1997-02-18 14:27:53 +00:00
if (!config) {
config = (struct client_config *)
2000-01-25 00:58:02 +00:00
dmalloc (sizeof (struct client_config),
MDL);
1997-02-18 14:27:53 +00:00
if (!config)
log_fatal ("no memory for client config.");
1997-02-18 14:27:53 +00:00
memcpy (config, &top_level_config,
sizeof top_level_config);
}
ip -> client -> config = config;
}
}
return status;
1997-02-18 14:27:53 +00:00
}
/* lease-file :== client-lease-statements EOF
client-lease-statements :== <nil>
1997-02-22 12:25:32 +00:00
| client-lease-statements LEASE client-lease-statement */
1997-02-18 14:27:53 +00:00
void read_client_leases ()
{
int file;
struct parse *cfile;
const char *val;
1997-02-18 14:27:53 +00:00
int token;
/* Open the lease file. If we can't open it, just return -
we can safely trust the server to remember our state. */
if ((file = open (path_dhclient_db, O_RDONLY)) < 0)
1997-02-18 14:27:53 +00:00
return;
cfile = (struct parse *)0;
new_parse (&cfile, file, (char *)0, 0, path_dhclient_db);
1997-02-18 14:27:53 +00:00
do {
token = next_token (&val, cfile);
if (token == EOF)
break;
if (token != LEASE) {
log_error ("Corrupt lease file - possible data loss!");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
break;
} else
parse_client_lease_statement (cfile, 0);
1997-02-18 14:27:53 +00:00
} while (1);
close (file);
end_parse (&cfile);
1997-02-18 14:27:53 +00:00
}
/* client-declaration :==
SEND option-decl |
DEFAULT option-decl |
1997-10-27 20:13:21 +00:00
SUPERSEDE option-decl |
PREPEND option-decl |
APPEND option-decl |
1997-02-18 14:27:53 +00:00
hardware-declaration |
REQUEST option-list |
REQUIRE option-list |
TIMEOUT number |
RETRY number |
REBOOT number |
1997-02-18 14:27:53 +00:00
SELECT_TIMEOUT number |
SCRIPT string |
interface-declaration |
1997-02-22 12:25:32 +00:00
LEASE client-lease-statement |
ALIAS client-lease-statement |
AUTH_KEY key_id key_data */
1997-02-18 14:27:53 +00:00
void parse_client_statement (cfile, ip, config)
struct parse *cfile;
1997-02-18 14:27:53 +00:00
struct interface_info *ip;
struct client_config *config;
{
int token;
const char *val;
struct option *option;
1998-10-22 04:52:23 +00:00
struct executable_statement *stmt, **p;
enum statement_op op;
1998-11-06 00:10:58 +00:00
int lose;
1998-11-09 02:43:23 +00:00
char *name;
struct data_string key_id;
enum policy policy;
int known;
1997-02-18 14:27:53 +00:00
1998-11-06 00:10:58 +00:00
switch (peek_token (&val, cfile)) {
case AUTH_KEY:
next_token (&val, cfile);
if (ip) {
/* This may seem arbitrary, but there's a reason for
doing it: the authentication key database is not
scoped. If we allow the user to declare a key other
than in the outer scope, the user is very likely to
believe that the key will only be used in that
scope. If the user only wants the key to be used on
one interface, because it's known that the other
interface may be connected to an insecure net and
the secret key is considered sensitive, we don't
want to lull them into believing they've gotten
their way. This is a bit contrived, but people
tend not to be entirely rational about security. */
parse_warn (cfile, "auth-key not allowed here.");
skip_to_semi (cfile);
break;
}
memset (&key_id, 0, sizeof key_id);
if (parse_auth_key (&key_id, cfile))
2000-01-25 00:58:02 +00:00
data_string_forget (&key_id, MDL);
return;
/* REQUIRE can either start a policy statement or a
comma-seperated list of names of required options. */
case REQUIRE:
next_token (&val, cfile);
token = peek_token (&val, cfile);
if (token == AUTHENTICATION) {
policy = P_REQUIRE;
goto do_policy;
}
parse_option_list (cfile, &config -> required_options);
return;
case IGNORE:
next_token (&val, cfile);
policy = P_IGNORE;
goto do_policy;
case ACCEPT:
next_token (&val, cfile);
policy = P_ACCEPT;
goto do_policy;
case PREFER:
next_token (&val, cfile);
policy = P_PREFER;
goto do_policy;
case DONT:
next_token (&val, cfile);
policy = P_DONT;
goto do_policy;
do_policy:
token = next_token (&val, cfile);
if (token == AUTHENTICATION) {
if (policy != P_PREFER &&
policy != P_REQUIRE &&
policy != P_DONT) {
parse_warn (cfile,
"invalid authentication policy.");
skip_to_semi (cfile);
return;
}
config -> auth_policy = policy;
} else if (token != BOOTP) {
if (policy != P_PREFER &&
policy != P_IGNORE &&
policy != P_ACCEPT) {
parse_warn (cfile, "invalid bootp policy.");
skip_to_semi (cfile);
return;
}
config -> bootp_policy = policy;
} else {
parse_warn (cfile, "expecting a policy type.");
skip_to_semi (cfile);
return;
}
break;
case SEND:
1998-11-11 07:48:23 +00:00
p = &config -> on_transmission -> statements;
1998-11-06 00:10:58 +00:00
op = supersede_option_statement;
1998-10-22 04:52:23 +00:00
do_option:
token = next_token (&val, cfile);
known = 0;
option = parse_option_name (cfile, 0, &known);
if (!option)
return;
1999-07-17 17:59:02 +00:00
stmt = (struct executable_statement *)0;
if (!parse_option_statement (&stmt, cfile, 1, option, op))
return;
1998-10-22 04:52:23 +00:00
for (; *p; p = &((*p) -> next))
;
executable_statement_reference (p, stmt, MDL);
1998-10-22 04:52:23 +00:00
stmt -> next = (struct executable_statement *)0;
1997-02-18 14:27:53 +00:00
return;
1999-03-25 21:45:55 +00:00
case OPTION:
token = next_token (&val, cfile);
token = peek_token (&val, cfile);
if (token == SPACE) {
if (ip) {
parse_warn (cfile,
"option space definitions %s",
" may not be scoped.");
skip_to_semi (cfile);
break;
}
parse_option_space_decl (cfile);
return;
}
option = parse_option_name (cfile, 1, &known);
1999-03-25 21:45:55 +00:00
if (!option)
return;
token = next_token (&val, cfile);
if (token != CODE) {
parse_warn (cfile, "expecting \"code\" keyword.");
1999-03-25 21:45:55 +00:00
skip_to_semi (cfile);
2000-01-25 00:58:02 +00:00
free_option (option, MDL);
1999-03-25 21:45:55 +00:00
return;
}
if (ip) {
parse_warn (cfile,
"option definitions may only appear in %s",
1999-03-25 21:45:55 +00:00
"the outermost scope.");
skip_to_semi (cfile);
2000-01-25 00:58:02 +00:00
free_option (option, MDL);
1999-03-25 21:45:55 +00:00
return;
}
if (!parse_option_code_definition (cfile, option))
2000-01-25 00:58:02 +00:00
free_option (option, MDL);
1999-03-25 21:45:55 +00:00
return;
case DEFAULT:
1998-11-11 07:48:23 +00:00
p = &config -> on_receipt -> statements;
1998-10-22 04:52:23 +00:00
op = default_option_statement;
goto do_option;
case SUPERSEDE:
1998-11-11 07:48:23 +00:00
p = &config -> on_receipt -> statements;
1998-10-22 04:52:23 +00:00
op = supersede_option_statement;
goto do_option;
case APPEND:
1998-11-11 07:48:23 +00:00
p = &config -> on_receipt -> statements;
1998-10-22 04:52:23 +00:00
op = append_option_statement;
goto do_option;
case PREPEND:
1998-11-11 07:48:23 +00:00
p = &config -> on_receipt -> statements;
1998-10-22 04:52:23 +00:00
op = prepend_option_statement;
goto do_option;
case MEDIA:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
parse_string_list (cfile, &config -> media, 1);
return;
1997-02-18 14:27:53 +00:00
case HARDWARE:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-18 14:27:53 +00:00
if (ip) {
parse_hardware_param (cfile, &ip -> hw_address);
} else {
parse_warn (cfile, "hardware address parameter %s",
1997-02-18 14:27:53 +00:00
"not allowed here.");
skip_to_semi (cfile);
}
return;
case REQUEST:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1999-10-01 03:42:31 +00:00
if (config -> requested_options == default_requested_options)
config -> requested_options = (u_int32_t *)0;
parse_option_list (cfile, &config -> requested_options);
1997-02-18 14:27:53 +00:00
return;
case TIMEOUT:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-18 14:27:53 +00:00
parse_lease_time (cfile, &config -> timeout);
return;
case RETRY:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-18 14:27:53 +00:00
parse_lease_time (cfile, &config -> retry_interval);
return;
case SELECT_TIMEOUT:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-18 14:27:53 +00:00
parse_lease_time (cfile, &config -> select_interval);
return;
case REBOOT:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
parse_lease_time (cfile, &config -> reboot_timeout);
return;
case BACKOFF_CUTOFF:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
parse_lease_time (cfile, &config -> backoff_cutoff);
return;
case INITIAL_INTERVAL:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
parse_lease_time (cfile, &config -> initial_interval);
return;
1997-02-18 14:27:53 +00:00
case SCRIPT:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-18 14:27:53 +00:00
config -> script_name = parse_string (cfile);
return;
case INTERFACE:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-18 14:27:53 +00:00
if (ip)
parse_warn (cfile, "nested interface declaration.");
1998-11-09 02:43:23 +00:00
parse_interface_declaration (cfile, config, (char *)0);
1997-02-18 14:27:53 +00:00
return;
1998-11-09 02:43:23 +00:00
case PSEUDO:
token = next_token (&val, cfile);
token = next_token (&val, cfile);
2000-01-25 00:58:02 +00:00
name = dmalloc (strlen (val) + 1, MDL);
1998-11-09 02:43:23 +00:00
if (!name)
log_fatal ("no memory for pseudo interface name");
1998-11-09 02:43:23 +00:00
strcpy (name, val);
parse_interface_declaration (cfile, config, name);
return;
1997-02-18 14:27:53 +00:00
case LEASE:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
parse_client_lease_statement (cfile, 1);
1997-02-18 14:27:53 +00:00
return;
1997-02-22 12:25:32 +00:00
case ALIAS:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-02-22 12:25:32 +00:00
parse_client_lease_statement (cfile, 2);
return;
1997-06-02 22:34:19 +00:00
case REJECT:
1998-11-06 00:10:58 +00:00
token = next_token (&val, cfile);
1997-06-02 22:34:19 +00:00
parse_reject_statement (cfile, config);
return;
1997-02-18 14:27:53 +00:00
default:
1998-11-06 00:10:58 +00:00
lose = 0;
1999-07-17 17:59:02 +00:00
stmt = (struct executable_statement *)0;
if (!parse_executable_statement (&stmt,
cfile, &lose, context_any)) {
1998-11-06 00:10:58 +00:00
if (!lose) {
parse_warn (cfile, "expecting a statement.");
1998-11-06 00:10:58 +00:00
skip_to_semi (cfile);
}
} else {
1998-11-11 07:48:23 +00:00
if (!config -> on_receipt -> statements) {
1999-07-17 17:59:02 +00:00
executable_statement_reference
(&config -> on_receipt -> statements,
2000-01-25 00:58:02 +00:00
stmt, MDL);
1998-11-06 00:10:58 +00:00
} else {
struct executable_statement *s;
1998-11-11 07:48:23 +00:00
for (s = config -> on_receipt -> statements;
1998-11-06 00:10:58 +00:00
s -> next; s = s -> next)
;
2000-01-25 00:58:02 +00:00
executable_statement_reference (&s -> next,
stmt, MDL);
1998-11-06 00:10:58 +00:00
}
return;
}
1997-02-18 14:27:53 +00:00
break;
}
1998-11-06 00:10:58 +00:00
parse_semi (cfile);
1997-02-18 14:27:53 +00:00
}
int parse_X (cfile, buf, max)
struct parse *cfile;
u_int8_t *buf;
unsigned max;
1997-02-18 14:27:53 +00:00
{
int token;
const char *val;
unsigned len;
u_int8_t *s;
1997-02-18 14:27:53 +00:00
token = peek_token (&val, cfile);
if (token == NUMBER_OR_NAME || token == NUMBER) {
len = 0;
1997-02-18 14:27:53 +00:00
do {
token = next_token (&val, cfile);
if (token != NUMBER && token != NUMBER_OR_NAME) {
parse_warn (cfile,
"expecting hexadecimal constant.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return 0;
}
convert_num (cfile, &buf [len], val, 16, 8);
if (len++ > max) {
parse_warn (cfile,
"hexadecimal constant too long.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return 0;
}
token = peek_token (&val, cfile);
if (token == COLON)
token = next_token (&val, cfile);
} while (token == COLON);
1997-02-27 03:39:11 +00:00
val = (char *)buf;
1997-02-18 14:27:53 +00:00
} else if (token == STRING) {
token = next_token (&val, cfile);
len = strlen (val);
if (len + 1 > max) {
parse_warn (cfile, "string constant too long.");
skip_to_semi (cfile);
return 0;
}
memcpy (buf, val, len + 1);
1997-02-18 14:27:53 +00:00
} else {
parse_warn (cfile, "expecting string or hexadecimal data");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return 0;
}
return len;
1997-02-18 14:27:53 +00:00
}
/* option-list :== option_name |
option_list COMMA option_name */
void parse_option_list (cfile, list)
struct parse *cfile;
u_int32_t **list;
1997-02-18 14:27:53 +00:00
{
int ix, i;
int token;
const char *val;
pair p = (pair)0, q, r;
1997-02-18 14:27:53 +00:00
ix = 0;
do {
token = next_token (&val, cfile);
if (token == SEMI)
break;
1997-02-18 14:27:53 +00:00
if (!is_identifier (token)) {
2000-01-25 00:58:02 +00:00
parse_warn (cfile, "%s: expected option name.", val);
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return;
1997-02-18 14:27:53 +00:00
}
for (i = 0; i < 256; i++) {
if (!strcasecmp (dhcp_options [i].name, val))
break;
}
if (i == 256) {
parse_warn (cfile, "%s: expected option name.", val);
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return;
1997-02-18 14:27:53 +00:00
}
2000-01-25 00:58:02 +00:00
r = new_pair (MDL);
if (!r)
log_fatal ("can't allocate pair for option code.");
r -> car = (caddr_t)i;
r -> cdr = (pair)0;
if (p)
q -> cdr = r;
else
p = r;
q = r;
++ix;
1997-02-18 14:27:53 +00:00
token = next_token (&val, cfile);
} while (token == COMMA);
if (token != SEMI) {
parse_warn (cfile, "expecting semicolon.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return;
}
if (*list)
2000-01-25 00:58:02 +00:00
dfree (*list, MDL);
if (ix) {
2000-01-25 00:58:02 +00:00
*list = dmalloc ((ix + 1) * sizeof **list, MDL);
if (!*list)
log_error ("no memory for option list.");
else {
ix = 0;
for (q = p; q; q = q -> cdr)
(*list) [ix++] = (u_int32_t)q -> car;
(*list) [ix] = 0;
}
while (p) {
q = p -> cdr;
2000-01-25 00:58:02 +00:00
free_pair (p, MDL);
p = q;
}
1997-02-18 14:27:53 +00:00
}
}
/* interface-declaration :==
INTERFACE string LBRACE client-declarations RBRACE */
1998-11-09 02:43:23 +00:00
void parse_interface_declaration (cfile, outer_config, name)
struct parse *cfile;
1997-02-18 14:27:53 +00:00
struct client_config *outer_config;
1998-11-09 02:43:23 +00:00
char *name;
1997-02-18 14:27:53 +00:00
{
int token;
const char *val;
1998-11-09 02:43:23 +00:00
struct client_state *client, **cp;
struct interface_info *ip;
1997-02-18 14:27:53 +00:00
token = next_token (&val, cfile);
if (token != STRING) {
parse_warn (cfile, "expecting interface name (in quotes).");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return;
}
ip = interface_or_dummy (val);
1997-02-18 14:27:53 +00:00
1998-11-09 02:43:23 +00:00
/* If we were given a name, this is a pseudo-interface. */
if (name) {
make_client_state (&client);
client -> name = name;
client -> interface = ip;
for (cp = &ip -> client; *cp; cp = &((*cp) -> next))
;
*cp = client;
} else {
if (!ip -> client) {
make_client_state (&ip -> client);
ip -> client -> interface = ip;
}
client = ip -> client;
}
1998-11-09 02:43:23 +00:00
if (!client -> config)
make_client_config (client, outer_config);
1997-02-18 14:27:53 +00:00
ip -> flags &= ~INTERFACE_AUTOMATIC;
interfaces_requested = 1;
1997-02-18 14:27:53 +00:00
token = next_token (&val, cfile);
if (token != LBRACE) {
parse_warn (cfile, "expecting left brace.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return;
}
do {
token = peek_token (&val, cfile);
if (token == EOF) {
parse_warn (cfile,
"unterminated interface declaration.");
1997-02-18 14:27:53 +00:00
return;
}
if (token == RBRACE)
break;
1998-11-09 02:43:23 +00:00
parse_client_statement (cfile, ip, client -> config);
1997-02-18 14:27:53 +00:00
} while (1);
token = next_token (&val, cfile);
}
struct interface_info *interface_or_dummy (name)
const char *name;
{
struct interface_info *ip;
/* Find the interface (if any) that matches the name. */
for (ip = interfaces; ip; ip = ip -> next) {
if (!strcmp (ip -> name, name))
break;
}
/* If it's not a real interface, see if it's on the dummy list. */
if (!ip) {
for (ip = dummy_interfaces; ip; ip = ip -> next) {
if (!strcmp (ip -> name, name))
break;
}
}
/* If we didn't find an interface, make a dummy interface as
a placeholder. */
if (!ip) {
2000-01-25 00:58:02 +00:00
ip = (struct interface_info *)dmalloc (sizeof *ip, MDL);
if (!ip)
2000-01-25 00:58:02 +00:00
log_fatal ("No memory to record interface %s", name);
memset (ip, 0, sizeof *ip);
strcpy (ip -> name, name);
ip -> next = dummy_interfaces;
dummy_interfaces = ip;
}
return ip;
}
1998-11-09 02:43:23 +00:00
void make_client_state (state)
struct client_state **state;
{
2000-01-25 00:58:02 +00:00
*state = ((struct client_state *)dmalloc (sizeof **state, MDL));
1998-11-09 02:43:23 +00:00
if (!*state)
log_fatal ("no memory for client state\n");
1998-11-09 02:43:23 +00:00
memset (*state, 0, sizeof **state);
}
1998-11-09 02:43:23 +00:00
void make_client_config (client, config)
struct client_state *client;
struct client_config *config;
{
1998-11-09 02:43:23 +00:00
client -> config = (((struct client_config *)
2000-01-25 00:58:02 +00:00
dmalloc (sizeof (struct client_config), MDL)));
1998-11-09 02:43:23 +00:00
if (!client -> config)
log_fatal ("no memory for client config\n");
1998-11-09 02:43:23 +00:00
memcpy (client -> config, config, sizeof *config);
1998-11-11 07:48:23 +00:00
client -> config -> on_receipt =
2000-01-25 00:58:02 +00:00
clone_group (config -> on_receipt, MDL);
1998-11-11 07:48:23 +00:00
client -> config -> on_transmission =
2000-01-25 00:58:02 +00:00
clone_group (config -> on_transmission, MDL);
}
1997-02-18 14:27:53 +00:00
/* client-lease-statement :==
1997-02-22 12:25:32 +00:00
RBRACE client-lease-declarations LBRACE
1997-02-18 14:27:53 +00:00
client-lease-declarations :==
<nil> |
client-lease-declaration |
client-lease-declarations client-lease-declaration */
void parse_client_lease_statement (cfile, is_static)
struct parse *cfile;
int is_static;
1997-02-18 14:27:53 +00:00
{
struct client_lease *lease, *lp, *pl;
1998-11-09 02:43:23 +00:00
struct interface_info *ip = (struct interface_info *)0;
1997-02-18 14:27:53 +00:00
int token;
const char *val;
1998-11-09 02:43:23 +00:00
struct client_state *client = (struct client_state *)0;
1997-02-18 14:27:53 +00:00
token = next_token (&val, cfile);
if (token != LBRACE) {
parse_warn (cfile, "expecting left brace.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return;
}
2000-01-25 00:58:02 +00:00
lease = ((struct client_lease *)
dmalloc (sizeof (struct client_lease), MDL));
1997-02-18 14:27:53 +00:00
if (!lease)
log_fatal ("no memory for lease.\n");
1997-02-18 14:27:53 +00:00
memset (lease, 0, sizeof *lease);
lease -> is_static = is_static;
2000-01-25 00:58:02 +00:00
if (!option_state_allocate (&lease -> options, MDL))
log_fatal ("no memory for lease options.\n");
1997-02-18 14:27:53 +00:00
do {
token = peek_token (&val, cfile);
if (token == EOF) {
parse_warn (cfile, "unterminated lease declaration.");
1997-02-18 14:27:53 +00:00
return;
}
if (token == RBRACE)
break;
1998-11-09 02:43:23 +00:00
parse_client_lease_declaration (cfile, lease, &ip, &client);
1997-02-18 14:27:53 +00:00
} while (1);
token = next_token (&val, cfile);
/* If the lease declaration didn't include an interface
declaration that we recognized, it's of no use to us. */
if (!ip) {
destroy_client_lease (lease);
1997-02-18 14:27:53 +00:00
return;
}
/* Make sure there's a client state structure... */
1998-11-09 02:43:23 +00:00
if (!ip -> client) {
make_client_state (&ip -> client);
ip -> client -> interface = ip;
}
if (!client)
client = ip -> client;
1997-02-22 12:25:32 +00:00
/* If this is an alias lease, it doesn't need to be sorted in. */
if (is_static == 2) {
ip -> client -> alias = lease;
return;
}
/* The new lease may supersede a lease that's not the
active lease but is still on the lease list, so scan the
lease list looking for a lease with the same address, and
if we find it, toss it. */
pl = (struct client_lease *)0;
1998-11-09 02:43:23 +00:00
for (lp = client -> leases; lp; lp = lp -> next) {
if (lp -> address.len == lease -> address.len &&
!memcmp (lp -> address.iabuf, lease -> address.iabuf,
lease -> address.len)) {
if (pl)
pl -> next = lp -> next;
else
1998-11-09 02:43:23 +00:00
client -> leases = lp -> next;
destroy_client_lease (lp);
break;
}
}
/* If this is a preloaded lease, just put it on the list of recorded
leases - don't make it the active lease. */
if (is_static) {
1998-11-09 02:43:23 +00:00
lease -> next = client -> leases;
client -> leases = lease;
return;
}
1997-02-18 14:27:53 +00:00
/* The last lease in the lease file on a particular interface is
the active lease for that interface. Of course, we don't know
what the last lease in the file is until we've parsed the whole
file, so at this point, we assume that the lease we just parsed
is the active lease for its interface. If there's already
an active lease for the interface, and this lease is for the same
ip address, then we just toss the old active lease and replace
it with this one. If this lease is for a different address,
then if the old active lease has expired, we dump it; if not,
we put it on the list of leases for this interface which are
still valid but no longer active. */
1998-11-09 02:43:23 +00:00
if (client -> active) {
if (client -> active -> expiry < cur_time)
destroy_client_lease (client -> active);
else if (client -> active -> address.len ==
1997-02-18 14:27:53 +00:00
lease -> address.len &&
1998-11-09 02:43:23 +00:00
!memcmp (client -> active -> address.iabuf,
1997-02-18 14:27:53 +00:00
lease -> address.iabuf,
lease -> address.len))
1998-11-09 02:43:23 +00:00
destroy_client_lease (client -> active);
1997-02-18 14:27:53 +00:00
else {
1998-11-09 02:43:23 +00:00
client -> active -> next = client -> leases;
client -> leases = client -> active;
1997-02-18 14:27:53 +00:00
}
}
1998-11-09 02:43:23 +00:00
client -> active = lease;
1997-02-18 14:27:53 +00:00
/* phew. */
}
/* client-lease-declaration :==
BOOTP |
1997-02-18 14:27:53 +00:00
INTERFACE string |
FIXED_ADDR ip_address |
FILENAME string |
SERVER_NAME string |
OPTION option-decl |
1997-02-18 14:27:53 +00:00
RENEW time-decl |
REBIND time-decl |
EXPIRE time-decl |
AUTH_KEY id */
1997-02-18 14:27:53 +00:00
1998-11-09 02:43:23 +00:00
void parse_client_lease_declaration (cfile, lease, ipp, clientp)
struct parse *cfile;
1997-02-18 14:27:53 +00:00
struct client_lease *lease;
struct interface_info **ipp;
1998-11-09 02:43:23 +00:00
struct client_state **clientp;
1997-02-18 14:27:53 +00:00
{
int token;
const char *val;
1997-02-18 14:27:53 +00:00
char *t, *n;
struct interface_info *ip;
struct option_cache *oc;
1998-11-09 02:43:23 +00:00
struct client_state *client = (struct client_state *)0;
struct data_string key_id;
1997-02-18 14:27:53 +00:00
switch (next_token (&val, cfile)) {
case AUTH_KEY:
memset (&key_id, 0, sizeof key_id);
if (parse_auth_key (&key_id, cfile)) {
2000-01-25 00:58:02 +00:00
data_string_copy (&lease -> auth_key_id, &key_id, MDL);
data_string_forget (&key_id, MDL);
}
break;
case BOOTP:
lease -> is_bootp = 1;
break;
1997-02-18 14:27:53 +00:00
case INTERFACE:
token = next_token (&val, cfile);
if (token != STRING) {
parse_warn (cfile,
"expecting interface name (in quotes).");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
break;
}
ip = interface_or_dummy (val);
1997-02-18 14:27:53 +00:00
*ipp = ip;
break;
1998-11-09 02:43:23 +00:00
case NAME:
token = next_token (&val, cfile);
ip = *ipp;
if (!ip) {
parse_warn (cfile, "state name precedes interface.");
1998-11-09 02:43:23 +00:00
break;
}
for (client = ip -> client; client; client = client -> next)
1998-11-11 07:48:23 +00:00
if (client -> name && !strcmp (client -> name, val))
1998-11-09 02:43:23 +00:00
break;
if (!client)
parse_warn (cfile,
"lease specified for unknown pseudo.");
1998-11-09 02:43:23 +00:00
*clientp = client;
break;
1997-02-18 14:27:53 +00:00
case FIXED_ADDR:
if (!parse_ip_addr (cfile, &lease -> address))
return;
1997-02-18 14:27:53 +00:00
break;
case MEDIUM:
parse_string_list (cfile, &lease -> medium, 0);
return;
1997-02-18 14:27:53 +00:00
case FILENAME:
lease -> filename = parse_string (cfile);
return;
case SERVER_NAME:
lease -> server_name = parse_string (cfile);
return;
case RENEW:
lease -> renewal = parse_date (cfile);
return;
case REBIND:
lease -> rebind = parse_date (cfile);
return;
case EXPIRE:
lease -> expiry = parse_date (cfile);
return;
case OPTION:
oc = (struct option_cache *)0;
if (parse_option_decl (&oc, cfile)) {
save_option (oc -> option -> universe,
lease -> options, oc);
2000-01-25 00:58:02 +00:00
option_cache_dereference (&oc, MDL);
}
1997-02-18 14:27:53 +00:00
return;
default:
parse_warn (cfile, "expecting lease declaration.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
break;
}
token = next_token (&val, cfile);
if (token != SEMI) {
parse_warn (cfile, "expecting semicolon.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
}
}
int parse_option_decl (oc, cfile)
struct option_cache **oc;
struct parse *cfile;
1997-02-18 14:27:53 +00:00
{
const char *val;
1997-02-18 14:27:53 +00:00
int token;
u_int8_t buf [4];
u_int8_t hunkbuf [1024];
unsigned hunkix = 0;
const char *fmt;
1997-02-18 14:27:53 +00:00
struct option *option;
struct iaddr ip_addr;
1997-02-27 03:39:11 +00:00
u_int8_t *dp;
unsigned len;
int nul_term = 0;
struct buffer *bp;
int known = 0;
1997-02-18 14:27:53 +00:00
option = parse_option_name (cfile, 0, &known);
if (!option)
return 0;
1997-02-18 14:27:53 +00:00
/* Parse the option data... */
do {
/* Set a flag if this is an array of a simple type (i.e.,
not an array of pairs of IP addresses, or something
like that. */
int uniform = option -> format [1] == 'A';
for (fmt = option -> format; *fmt; fmt++) {
if (*fmt == 'A')
break;
switch (*fmt) {
case 'X':
len = parse_X (cfile, &hunkbuf [hunkix],
sizeof hunkbuf - hunkix);
hunkix += len;
1997-02-18 14:27:53 +00:00
break;
case 't': /* Text string... */
token = next_token (&val, cfile);
if (token != STRING) {
parse_warn (cfile,
"expecting string.");
skip_to_semi (cfile);
return 0;
}
len = strlen (val);
if (hunkix + len + 1 > sizeof hunkbuf) {
parse_warn (cfile,
"option data buffer %s",
"overflow");
skip_to_semi (cfile);
return 0;
}
memcpy (&hunkbuf [hunkix], val, len + 1);
nul_term = 1;
hunkix += len;
1997-02-18 14:27:53 +00:00
break;
case 'I': /* IP address. */
if (!parse_ip_addr (cfile, &ip_addr))
return 0;
1997-02-18 14:27:53 +00:00
len = ip_addr.len;
dp = ip_addr.iabuf;
alloc:
if (hunkix + len > sizeof hunkbuf) {
parse_warn (cfile,
"option data buffer %s",
"overflow");
skip_to_semi (cfile);
return 0;
}
memcpy (&hunkbuf [hunkix], dp, len);
hunkix += len;
1997-02-18 14:27:53 +00:00
break;
case 'L': /* Unsigned 32-bit integer... */
case 'l': /* Signed 32-bit integer... */
token = next_token (&val, cfile);
if (token != NUMBER) {
need_number:
parse_warn (cfile,
"expecting number.");
1997-02-18 14:27:53 +00:00
if (token != SEMI)
skip_to_semi (cfile);
return 0;
1997-02-18 14:27:53 +00:00
}
convert_num (cfile, buf, val, 0, 32);
1997-02-18 14:27:53 +00:00
len = 4;
dp = buf;
goto alloc;
case 's': /* Signed 16-bit integer. */
case 'S': /* Unsigned 16-bit integer. */
token = next_token (&val, cfile);
if (token != NUMBER)
goto need_number;
convert_num (cfile, buf, val, 0, 16);
1997-02-18 14:27:53 +00:00
len = 2;
dp = buf;
goto alloc;
case 'b': /* Signed 8-bit integer. */
case 'B': /* Unsigned 8-bit integer. */
token = next_token (&val, cfile);
if (token != NUMBER)
goto need_number;
convert_num (cfile, buf, val, 0, 8);
1997-02-18 14:27:53 +00:00
len = 1;
dp = buf;
goto alloc;
case 'f': /* Boolean flag. */
token = next_token (&val, cfile);
if (!is_identifier (token)) {
parse_warn (cfile,
"expecting identifier.");
1997-02-18 14:27:53 +00:00
bad_flag:
if (token != SEMI)
skip_to_semi (cfile);
return 0;
1997-02-18 14:27:53 +00:00
}
if (!strcasecmp (val, "true")
|| !strcasecmp (val, "on"))
buf [0] = 1;
else if (!strcasecmp (val, "false")
|| !strcasecmp (val, "off"))
buf [0] = 0;
else {
parse_warn (cfile,
"expecting boolean.");
1997-02-18 14:27:53 +00:00
goto bad_flag;
}
len = 1;
dp = buf;
goto alloc;
default:
log_error ("parse_option_param: Bad format %c",
1997-02-18 14:27:53 +00:00
*fmt);
skip_to_semi (cfile);
return 0;
1997-02-18 14:27:53 +00:00
}
}
token = next_token (&val, cfile);
} while (*fmt == 'A' && token == COMMA);
1997-02-18 14:27:53 +00:00
if (token != SEMI) {
parse_warn (cfile, "semicolon expected.");
1997-02-18 14:27:53 +00:00
skip_to_semi (cfile);
return 0;
1997-02-18 14:27:53 +00:00
}
bp = (struct buffer *)0;
2000-01-25 00:58:02 +00:00
if (!buffer_allocate (&bp, hunkix + nul_term, MDL))
log_fatal ("no memory to store option declaration.");
if (!bp -> data)
log_fatal ("out of memory allocating option data.");
memcpy (bp -> data, hunkbuf, hunkix + nul_term);
2000-01-25 00:58:02 +00:00
if (!option_cache_allocate (oc, MDL))
log_fatal ("out of memory allocating option cache.");
(*oc) -> data.buffer = bp;
(*oc) -> data.data = &bp -> data [0];
(*oc) -> data.terminated = nul_term;
(*oc) -> data.len = hunkix;
(*oc) -> option = option;
return 1;
}
void parse_string_list (cfile, lp, multiple)
struct parse *cfile;
struct string_list **lp;
int multiple;
{
int token;
const char *val;
struct string_list *cur, *tmp;
/* Find the last medium in the media list. */
if (*lp) {
for (cur = *lp; cur -> next; cur = cur -> next)
;
} else {
cur = (struct string_list *)0;
}
do {
token = next_token (&val, cfile);
if (token != STRING) {
parse_warn (cfile, "Expecting media options.");
skip_to_semi (cfile);
return;
}
2000-01-25 00:58:02 +00:00
tmp = ((struct string_list *)
dmalloc (strlen (val) + 1 +
sizeof (struct string_list *), MDL));
if (!tmp)
log_fatal ("no memory for string list entry.");
strcpy (tmp -> string, val);
tmp -> next = (struct string_list *)0;
/* Store this medium at the end of the media list. */
if (cur)
cur -> next = tmp;
else
*lp = tmp;
cur = tmp;
token = next_token (&val, cfile);
} while (multiple && token == COMMA);
if (token != SEMI) {
parse_warn (cfile, "expecting semicolon.");
skip_to_semi (cfile);
}
}
1997-06-02 22:34:19 +00:00
void parse_reject_statement (cfile, config)
struct parse *cfile;
1997-06-02 22:34:19 +00:00
struct client_config *config;
{
int token;
const char *val;
1997-06-02 22:34:19 +00:00
struct iaddr addr;
struct iaddrlist *list;
do {
if (!parse_ip_addr (cfile, &addr)) {
parse_warn (cfile, "expecting IP address.");
1997-06-02 22:34:19 +00:00
skip_to_semi (cfile);
return;
}
2000-01-25 00:58:02 +00:00
list = (struct iaddrlist *)dmalloc (sizeof (struct iaddrlist),
MDL);
1997-06-02 22:34:19 +00:00
if (!list)
log_fatal ("no memory for reject list!");
1997-06-02 22:34:19 +00:00
list -> addr = addr;
list -> next = config -> reject_list;
config -> reject_list = list;
token = next_token (&val, cfile);
} while (token == COMMA);
if (token != SEMI) {
parse_warn (cfile, "expecting semicolon.");
1997-06-02 22:34:19 +00:00
skip_to_semi (cfile);
}
}
/* allow-deny-keyword :== BOOTP
| BOOTING
| DYNAMIC_BOOTP
| UNKNOWN_CLIENTS */
int parse_allow_deny (oc, cfile, flag)
struct option_cache **oc;
struct parse *cfile;
int flag;
{
enum dhcp_token token;
const char *val;
unsigned char rf = flag;
struct expression *data = (struct expression *)0;
int status;
parse_warn (cfile, "allow/deny/ignore not permitted here.");
skip_to_semi (cfile);
return 0;
}