2
0
mirror of https://gitlab.isc.org/isc-projects/dhcp synced 2025-08-23 10:28:08 +00:00
isc-dhcp/server/confpars.c

1192 lines
29 KiB
C
Raw Normal View History

1995-11-29 07:40:04 +00:00
/* confpars.c
Parser for dhcpd config file... */
/*
* Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
* All rights reserved.
1995-11-29 07:40:04 +00:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of The Internet Software Consortium nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This software has been written for the Internet Software Consortium
* by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
* Enterprises. To learn more about the Internet Software Consortium,
* see ``http://www.vix.com/isc''. To learn more about Vixie
* Enterprises, see ``http://www.vix.com''.
*/
#ifndef lint
static char copyright[] =
"$Id: confpars.c,v 1.51 1998/06/25 03:51:59 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n";
1995-11-29 07:40:04 +00:00
#endif /* not lint */
#include "dhcpd.h"
#include "dhctoken.h"
static TIME parsed_time;
/* conf-file :== parameters declarations EOF
parameters :== <nil> | parameter | parameters parameter
declarations :== <nil> | declaration | declarations declaration */
1995-11-29 07:40:04 +00:00
int readconf ()
1995-11-29 07:40:04 +00:00
{
FILE *cfile;
char *val;
int token;
int declaration = 0;
1995-11-29 07:40:04 +00:00
1996-09-02 21:16:25 +00:00
new_parse (path_dhcpd_conf);
1995-11-29 07:40:04 +00:00
/* Set up the initial dhcp option universe. */
initialize_universes ();
1996-09-02 21:16:25 +00:00
if ((cfile = fopen (path_dhcpd_conf, "r")) == NULL)
error ("Can't open %s: %m", path_dhcpd_conf);
1995-11-29 07:40:04 +00:00
do {
token = peek_token (&val, cfile);
if (token == EOF)
break;
declaration = parse_statement (cfile, &root_group,
ROOT_GROUP,
(struct host_decl *)0,
declaration);
1995-11-29 07:40:04 +00:00
} while (1);
token = next_token (&val, cfile); /* Clear the peek buffer */
return !warnings_occurred;
1996-03-02 05:13:36 +00:00
}
/* lease-file :== lease-declarations EOF
lease-statments :== <nil>
| lease-declaration
| lease-declarations lease-declaration */
void read_leases ()
1996-03-02 05:13:36 +00:00
{
FILE *cfile;
char *val;
int token;
1996-09-02 21:16:25 +00:00
new_parse (path_dhcpd_db);
1996-08-27 09:40:17 +00:00
/* Open the lease file. If we can't open it, fail. The reason
for this is that although on initial startup, the absence of
a lease file is perfectly benign, if dhcpd has been running
and this file is absent, it means that dhcpd tried and failed
to rewrite the lease database. If we proceed and the
problem which caused the rewrite to fail has been fixed, but no
human has corrected the database problem, then we are left
thinking that no leases have been assigned to anybody, which
could create severe network chaos. */
1996-09-02 21:16:25 +00:00
if ((cfile = fopen (path_dhcpd_db, "r")) == NULL)
1996-08-27 09:40:17 +00:00
error ("Can't open lease database %s: %m -- %s",
1996-09-02 21:16:25 +00:00
path_dhcpd_db,
1996-08-27 09:40:17 +00:00
"check for failed database rewrite attempt!");
1996-03-02 05:13:36 +00:00
do {
token = next_token (&val, cfile);
if (token == EOF)
break;
if (token != LEASE) {
warn ("Corrupt lease file - possible data loss!");
skip_to_semi (cfile);
} else {
1996-08-27 09:40:17 +00:00
struct lease *lease;
lease = parse_lease_declaration (cfile);
1996-08-27 09:40:17 +00:00
if (lease)
1996-03-02 05:13:36 +00:00
enter_lease (lease);
1996-08-27 09:40:17 +00:00
else
parse_warn ("possibly corrupt lease file");
1996-03-02 05:13:36 +00:00
}
} while (1);
1995-11-29 07:40:04 +00:00
}
/* statement :== parameter | declaration
parameter :== timestamp
| DEFAULT_LEASE_TIME lease_time
| MAX_LEASE_TIME lease_time
| DYNAMIC_BOOTP_LEASE_CUTOFF date
| DYNAMIC_BOOTP_LEASE_LENGTH lease_time
| BOOT_UNKNOWN_CLIENTS boolean
| ONE_LEASE_PER_CLIENT boolean
1996-08-29 23:02:40 +00:00
| GET_LEASE_HOSTNAMES boolean
1996-09-09 07:04:29 +00:00
| USE_HOST_DECL_NAME boolean
| NEXT_SERVER ip-addr-or-hostname SEMI
| option_parameter
| SERVER-IDENTIFIER ip-addr-or-hostname SEMI
| FILENAME string-parameter
| SERVER_NAME string-parameter
| hardware-parameter
| fixed-address-parameter
| ALLOW allow-deny-keyword
| DENY allow-deny-keyword
| USE_LEASE_ADDR_FOR_DEFAULT_ROUTE boolean
declaration :== host-declaration
| group-declaration
| shared-network-declaration
| subnet-declaration
| VENDOR_CLASS class-declaration
| USER_CLASS class-declaration
| RANGE address-range-declaration */
int parse_statement (cfile, group, type, host_decl, declaration)
1995-11-29 07:40:04 +00:00
FILE *cfile;
1996-08-27 09:40:17 +00:00
struct group *group;
int type;
struct host_decl *host_decl;
int declaration;
1995-11-29 07:40:04 +00:00
{
int token;
1995-11-29 07:40:04 +00:00
char *val;
1996-08-27 09:40:17 +00:00
struct shared_network *share;
char *t, *n;
struct expression *expr;
struct data_string data;
1996-08-27 09:40:17 +00:00
struct hardware hardware;
struct executable_statement *et, *ep;
struct option *option;
struct option_cache *cache;
int lose;
1995-11-29 07:40:04 +00:00
switch (peek_token (&val, cfile)) {
1995-11-29 07:40:04 +00:00
case HOST:
next_token (&val, cfile);
if (type != HOST_DECL && type != CLASS_DECL)
parse_host_declaration (cfile, group);
1996-08-27 09:40:17 +00:00
else {
parse_warn ("host declarations not allowed here.");
1996-08-27 09:40:17 +00:00
skip_to_semi (cfile);
1995-11-29 07:40:04 +00:00
}
1996-08-27 09:40:17 +00:00
return 1;
case GROUP:
next_token (&val, cfile);
if (type != HOST_DECL && type != CLASS_DECL)
parse_group_declaration (cfile, group);
1996-08-27 09:40:17 +00:00
else {
parse_warn ("host declarations not allowed here.");
1996-08-27 09:40:17 +00:00
skip_to_semi (cfile);
1995-11-29 07:40:04 +00:00
}
1996-08-27 09:40:17 +00:00
return 1;
1995-11-29 07:40:04 +00:00
case TIMESTAMP:
next_token (&val, cfile);
1996-08-27 09:40:17 +00:00
parsed_time = parse_timestamp (cfile);
1995-11-29 07:40:04 +00:00
break;
1996-08-27 09:40:17 +00:00
case SHARED_NETWORK:
next_token (&val, cfile);
if (type == SHARED_NET_DECL ||
type == HOST_DECL ||
type == SUBNET_DECL ||
type == CLASS_DECL) {
parse_warn ("shared-network parameters not %s.",
1996-08-27 09:40:17 +00:00
"allowed here");
skip_to_semi (cfile);
break;
}
1996-08-27 09:40:17 +00:00
parse_shared_net_declaration (cfile, group);
1996-08-27 09:40:17 +00:00
return 1;
case SUBNET:
next_token (&val, cfile);
if (type == HOST_DECL || type == SUBNET_DECL ||
type == CLASS_DECL) {
parse_warn ("subnet declarations not allowed here.");
1996-08-27 09:40:17 +00:00
skip_to_semi (cfile);
return 1;
}
/* If we're in a subnet declaration, just do the parse. */
1996-08-27 09:40:17 +00:00
if (group -> shared_network) {
parse_subnet_declaration (cfile,
group -> shared_network);
1996-08-27 09:40:17 +00:00
break;
}
/* Otherwise, cons up a fake shared network structure
and populate it with the lone subnet... */
share = new_shared_network ("parse_statement");
if (!share)
error ("No memory for shared subnet");
share -> group = clone_group (group, "parse_statement:subnet");
share -> group -> shared_network = share;
parse_subnet_declaration (cfile, share);
1996-08-27 09:40:17 +00:00
if (share -> subnets) {
share -> interface =
share -> subnets -> interface;
n = piaddr (share -> subnets -> net);
t = malloc (strlen (n) + 1);
if (!t)
error ("no memory for subnet name");
strcpy (t, n);
share -> name = t;
enter_shared_network (share);
1995-11-29 07:40:04 +00:00
}
1996-08-27 09:40:17 +00:00
return 1;
case VENDOR_CLASS:
next_token (&val, cfile);
if (type == CLASS_DECL) {
parse_warn ("class declarations not allowed here.");
skip_to_semi (cfile);
break;
}
parse_class_declaration (cfile, group, 0);
1996-08-27 09:40:17 +00:00
return 1;
case USER_CLASS:
next_token (&val, cfile);
if (type == CLASS_DECL) {
parse_warn ("class declarations not allowed here.");
skip_to_semi (cfile);
break;
}
parse_class_declaration (cfile, group, 1);
1996-08-27 09:40:17 +00:00
return 1;
case CLASS:
next_token (&val, cfile);
if (type == CLASS_DECL) {
parse_warn ("class declarations not allowed here.");
skip_to_semi (cfile);
break;
}
parse_class_declaration (cfile, group, 2);
return 1;
case SUBCLASS:
next_token (&val, cfile);
if (type == CLASS_DECL) {
parse_warn ("class declarations not allowed here.");
skip_to_semi (cfile);
1996-08-27 09:40:17 +00:00
break;
}
parse_class_declaration (cfile, group, 3);
return 1;
1996-08-27 09:40:17 +00:00
case HARDWARE:
next_token (&val, cfile);
parse_hardware_param (cfile, &hardware);
1996-08-27 09:40:17 +00:00
if (host_decl)
host_decl -> interface = hardware;
else
parse_warn ("hardware address parameter %s",
1996-08-27 09:40:17 +00:00
"not allowed here.");
break;
case FIXED_ADDR:
next_token (&val, cfile);
cache = parse_fixed_addr_param (cfile);
1996-08-27 09:40:17 +00:00
if (host_decl)
host_decl -> fixed_addr = cache;
else
parse_warn ("fixed-address parameter not %s",
1996-08-27 09:40:17 +00:00
"allowed here.");
break;
case RANGE:
next_token (&val, cfile);
if (type != SUBNET_DECL || !group -> subnet) {
parse_warn ("range declaration not allowed here.");
1996-08-27 09:40:17 +00:00
skip_to_semi (cfile);
return declaration;
1996-08-27 09:40:17 +00:00
}
parse_address_range (cfile, group -> subnet);
return declaration;
1996-08-27 09:40:17 +00:00
case ALLOW:
case DENY:
token = next_token (&val, cfile);
cache = parse_allow_deny (cfile,
token == ALLOW ? 1 : 0);
et = (struct executable_statement *)dmalloc (sizeof *et,
"allow/deny");
if (!et)
error ("no memory for %s statement",
token == ALLOW ? "allow" : "deny");
memset (et, 0, sizeof *et);
et -> op = supersede_option_statement;
et -> data.option = cache;
goto insert_statement;
1995-11-29 07:40:04 +00:00
default:
et = (struct executable_statement *)0;
if (is_identifier (token)) {
option = ((struct option *)
hash_lookup (server_universe.hash,
(unsigned char *)val, 0));
if (option) {
et = parse_option_statement
(cfile, 1, option,
supersede_option_statement);
if (!et)
return declaration;
}
}
if (!et) {
lose = 0;
et = parse_executable_statement (cfile, &lose);
if (!et) {
if (declaration && !lose)
parse_warn ("expecting a %s.",
"declaration");
else
parse_warn ("expecting a parameter%s.",
" or declaration");
skip_to_semi (cfile);
return declaration;
}
}
if (!et) {
parse_warn ("expecting a %sdeclaration",
declaration ? "" : "parameter or ");
return declaration;
}
insert_statement:
if (group -> statements) {
for (ep = group -> statements; ep -> next;
ep = ep -> next)
;
ep -> next = et;
}
1995-11-29 07:40:04 +00:00
}
if (declaration) {
parse_warn ("parameters not allowed after first declaration.");
1996-08-27 09:40:17 +00:00
return 1;
}
1996-08-27 09:40:17 +00:00
return 0;
1995-11-29 07:40:04 +00:00
}
/* allow-deny-keyword :== BOOTP
| BOOTING
| DYNAMIC_BOOTP
| UNKNOWN_CLIENTS */
struct option_cache *parse_allow_deny (cfile, flag)
FILE *cfile;
int flag;
{
int token;
char *val;
char rf = flag;
struct option_cache *oc;
token = next_token (&val, cfile);
switch (token) {
case BOOTP:
oc = option_cache (make_const_data (&rf, 1, 0, 1),
&server_options [SV_ALLOW_BOOTP]);
break;
case BOOTING:
oc = option_cache (make_const_data (&rf, 1, 0, 1),
&server_options [SV_ALLOW_BOOTING]);
break;
case DYNAMIC_BOOTP:
oc = option_cache (make_const_data (&rf, 1, 0, 1),
&server_options [SV_DYNAMIC_BOOTP]);
break;
case UNKNOWN_CLIENTS:
oc = option_cache (make_const_data (&rf, 1, 0, 1),
&server_options [SV_BOOT_UNKNOWN_CLIENTS]);
break;
default:
parse_warn ("expecting allow/deny key");
skip_to_semi (cfile);
return (struct option_cache *)0;
}
parse_semi (cfile);
return oc;
}
/* boolean :== ON SEMI | OFF SEMI | TRUE SEMI | FALSE SEMI */
int parse_boolean (cfile)
FILE *cfile;
{
int token;
char *val;
int rv;
token = next_token (&val, cfile);
if (!strcasecmp (val, "true")
|| !strcasecmp (val, "on"))
rv = 1;
else if (!strcasecmp (val, "false")
|| !strcasecmp (val, "off"))
rv = 0;
else {
parse_warn ("boolean value (true/false/on/off) expected");
skip_to_semi (cfile);
return 0;
}
parse_semi (cfile);
return rv;
}
1996-08-27 09:40:17 +00:00
/* Expect a left brace; if there isn't one, skip over the rest of the
statement and return zero; otherwise, return 1. */
int parse_lbrace (cfile)
FILE *cfile;
{
int token;
char *val;
token = next_token (&val, cfile);
if (token != LBRACE) {
parse_warn ("expecting left brace.");
skip_to_semi (cfile);
return 0;
}
return 1;
1995-11-29 07:40:04 +00:00
}
1996-08-27 09:40:17 +00:00
/* host-declaration :== hostname RBRACE parameters declarations LBRACE */
1995-11-29 07:40:04 +00:00
void parse_host_declaration (cfile, group)
1995-11-29 07:40:04 +00:00
FILE *cfile;
1996-08-27 09:40:17 +00:00
struct group *group;
1995-11-29 07:40:04 +00:00
{
char *val;
int token;
1996-08-27 09:40:17 +00:00
struct host_decl *host;
1998-03-17 06:20:51 +00:00
char *name;
int declaration = 0;
1996-08-27 09:40:17 +00:00
1998-03-17 06:20:51 +00:00
token = peek_token (&val, cfile);
if (token != LBRACE) {
name = parse_host_name (cfile);
if (!name)
return;
} else {
name = (char *)0;
}
1996-08-27 09:40:17 +00:00
host = (struct host_decl *)dmalloc (sizeof (struct host_decl),
"parse_host_declaration");
1996-08-27 09:40:17 +00:00
if (!host)
error ("can't allocate host decl struct %s.", name);
host -> name = name;
host -> group = clone_group (group, "parse_host_declaration");
1996-08-27 09:40:17 +00:00
if (!parse_lbrace (cfile))
return;
1995-11-29 07:40:04 +00:00
do {
token = peek_token (&val, cfile);
1996-08-27 09:40:17 +00:00
if (token == RBRACE) {
1995-11-29 07:40:04 +00:00
token = next_token (&val, cfile);
break;
}
if (token == EOF) {
token = next_token (&val, cfile);
parse_warn ("unexpected end of file");
break;
}
declaration = parse_statement (cfile, host -> group,
HOST_DECL, host,
declaration);
1995-11-29 07:40:04 +00:00
} while (1);
1996-08-27 09:40:17 +00:00
enter_host (host);
1995-11-29 07:40:04 +00:00
}
/* class-declaration :== STRING LBRACE parameters declarations RBRACE
*/
void parse_class_declaration (cfile, group, type)
FILE *cfile;
1996-08-27 09:40:17 +00:00
struct group *group;
int type;
{
char *val;
int token;
struct class *class, *pc;
1996-12-31 02:01:28 +00:00
int declaration = 0;
int lose;
struct data_string data;
char *name;
struct executable_statement *stmt = (struct executable_statement *)0;
struct expression *expr;
token = next_token (&val, cfile);
if (token != STRING) {
parse_warn ("Expecting class name");
skip_to_semi (cfile);
1996-08-27 09:40:17 +00:00
return;
}
/* See if there's already a class with the specified name. */
pc = (struct class *)find_class (val);
/* If this isn't a subclass, we're updating an existing class. */
if (pc && type != 0 && type != 1 && type != 3) {
class = pc;
pc = (struct class *)0;
}
/* If this _is_ a subclass, there _must_ be a class with the
same name. */
if (!pc && (type == 0 || type == 1 || type == 3)) {
parse_warn ("no class named %s", val);
skip_to_semi (cfile);
return;
}
/* The old vendor-class and user-class declarations had an implicit
match. We don't do the implicit match anymore. Instead, for
backward compatibility, we have an implicit-vendor-class and an
implicit-user-class. vendor-class and user-class declarations
are turned into subclasses of the implicit classes, and the
spawn expression of the implicit classes extracts the contents of
the vendor class or user class. */
if (type == 0 || type == 1) {
data.len = strlen (val);
data.data = dmalloc (data.len + 1, "parse_class_declaration");
data.buffer = (char *)0;
data.terminated = 1;
name = type ? "implicit-vendor-class" : "implicit-user-class";
} else if (type == 2) {
if (!(name = dmalloc (strlen (val) + 1,
"parse_class_declaration")))
error ("No memory for class name %s.", val);
strcpy (name, val);
} else {
name = (char *)0;
}
/* If this is a straight subclass, parse the hash string. */
if (type == 3) {
token = peek_token (&val, cfile);
if (token == STRING) {
token = next_token (&val, cfile);
data.len = strlen (val);
data.data = dmalloc (data.len + 1,
"parse_class_declaration");
data.buffer = (char *)0;
data.terminated = 1;
} else if (token == NUMBER_OR_NAME || token == NUMBER) {
data.data = parse_cshl (cfile, &data.len);
if (!data.data)
return;
data.terminated = 0;
data.buffer = 0;
}
}
/* See if there's already a class in the hash table matching the
hash data. */
if (type == 0 || type == 1 || type == 3)
class = ((struct class *)
hash_lookup (pc -> hash, data.data, data.len));
/* If we didn't find an existing class, allocate a new one. */
if (!class) {
/* Allocate the class structure... */
class = (struct class *)dmalloc (sizeof (struct class),
"parse_class_declaration");
if (!class)
error ("No memory for class %s.", val);
memset (class, 0, sizeof *class);
if (pc) {
class -> group =
clone_group (pc -> group,
"parse_class_declaration");
add_hash (pc -> hash,
data.data, data.len, (unsigned char *)class);
} else {
class -> group =
clone_group (group, "parse_class_declaration");
}
/* If this is an implicit vendor or user class, add a
statement that causes the vendor or user class ID to
be sent back in the reply. */
if (type == 0 || type == 1) {
stmt = ((struct executable_statement *)
dmalloc (sizeof (struct executable_statement),
"implicit user/vendor class"));
if (!stmt)
error ("no memory for class statement.");
memset (stmt, 0, sizeof *stmt);
stmt -> op = supersede_option_statement;
stmt -> data.option =
(option_cache
(make_const_data (data.data, data.len, 0, 1),
dhcp_universe.options
[type
? DHO_DHCP_CLASS_IDENTIFIER
: DHO_DHCP_USER_CLASS_ID]));
class -> statements = stmt;
}
}
1996-08-27 09:40:17 +00:00
if (!parse_lbrace (cfile))
return;
do {
token = peek_token (&val, cfile);
1996-08-27 09:40:17 +00:00
if (token == RBRACE) {
token = next_token (&val, cfile);
break;
} else if (token == EOF) {
token = next_token (&val, cfile);
parse_warn ("unexpected end of file");
break;
} else if (token == MATCH) {
if (pc) {
parse_warn ("invalid match in subclass.");
skip_to_semi (cfile);
break;
}
if (class -> expr) {
parse_warn ("can't override match.");
skip_to_semi (cfile);
break;
}
token = next_token (&val, cfile);
token = next_token (&val, cfile);
if (token != IF) {
parse_warn ("expecting if after match");
skip_to_semi (cfile);
break;
}
class -> expr =
parse_boolean_expression (cfile, &lose);
if (lose)
break;
} else if (token == SPAWN) {
if (pc) {
parse_warn ("invalid spawn in subclass.");
skip_to_semi (cfile);
break;
}
if (class -> spawn) {
parse_warn ("can't override spawn.");
skip_to_semi (cfile);
break;
}
token = next_token (&val, cfile);
token = next_token (&val, cfile);
if (token != WITH) {
parse_warn ("expecting with after spawn");
skip_to_semi (cfile);
break;
}
class -> spawn =
parse_data_expression (cfile, &lose);
if (lose)
break;
} else {
declaration = parse_statement (cfile, class -> group,
CLASS_DECL,
(struct host_decl *)0,
declaration);
}
} while (1);
}
/* shared-network-declaration :==
hostname LBRACE declarations parameters RBRACE */
void parse_shared_net_declaration (cfile, group)
FILE *cfile;
1996-08-27 09:40:17 +00:00
struct group *group;
{
char *val;
int token;
struct shared_network *share;
char *name;
int declaration = 0;
share = new_shared_network ("parse_shared_net_declaration");
if (!share)
error ("No memory for shared subnet");
share -> leases = (struct lease *)0;
share -> last_lease = (struct lease *)0;
share -> insertion_point = (struct lease *)0;
share -> next = (struct shared_network *)0;
1996-06-01 02:01:07 +00:00
share -> interface = (struct interface_info *)0;
share -> group = clone_group (group, "parse_shared_net_declaration");
1996-08-27 09:40:17 +00:00
share -> group -> shared_network = share;
/* Get the name of the shared network... */
1996-08-27 09:40:17 +00:00
token = peek_token (&val, cfile);
if (token == STRING) {
token = next_token (&val, cfile);
if (val [0] == 0) {
parse_warn ("zero-length shared network name");
val = "<no-name-given>";
}
name = malloc (strlen (val) + 1);
if (!name)
error ("no memory for shared network name");
strcpy (name, val);
} else {
name = parse_host_name (cfile);
if (!name)
return;
}
share -> name = name;
1996-08-27 09:40:17 +00:00
if (!parse_lbrace (cfile))
return;
do {
1996-08-27 09:40:17 +00:00
token = peek_token (&val, cfile);
if (token == RBRACE) {
token = next_token (&val, cfile);
if (!share -> subnets) {
parse_warn ("empty shared-network decl");
return;
}
enter_shared_network (share);
return;
} else if (token == EOF) {
token = next_token (&val, cfile);
parse_warn ("unexpected end of file");
break;
}
declaration = parse_statement (cfile, share -> group,
SHARED_NET_DECL,
(struct host_decl *)0,
declaration);
} while (1);
}
/* subnet-declaration :==
net NETMASK netmask RBRACE parameters declarations LBRACE */
void parse_subnet_declaration (cfile, share)
FILE *cfile;
struct shared_network *share;
{
char *val;
int token;
1996-08-27 09:40:17 +00:00
struct subnet *subnet, *t;
struct iaddr iaddr;
unsigned char addr [4];
int len = sizeof addr;
int declaration = 0;
subnet = new_subnet ("parse_subnet_declaration");
if (!subnet)
error ("No memory for new subnet");
subnet -> shared_network = share;
1996-08-27 09:40:17 +00:00
subnet -> group = clone_group (share -> group,
"parse_subnet_declaration");
1996-08-27 09:40:17 +00:00
subnet -> group -> subnet = subnet;
/* Get the network number... */
1996-08-27 09:40:17 +00:00
if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
return;
memcpy (iaddr.iabuf, addr, len);
iaddr.len = len;
subnet -> net = iaddr;
token = next_token (&val, cfile);
if (token != NETMASK) {
parse_warn ("Expecting netmask");
skip_to_semi (cfile);
1996-08-27 09:40:17 +00:00
return;
}
/* Get the netmask... */
1996-08-27 09:40:17 +00:00
if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
return;
memcpy (iaddr.iabuf, addr, len);
iaddr.len = len;
subnet -> netmask = iaddr;
enter_subnet (subnet);
1996-08-27 09:40:17 +00:00
if (!parse_lbrace (cfile))
return;
do {
token = peek_token (&val, cfile);
1996-08-27 09:40:17 +00:00
if (token == RBRACE) {
token = next_token (&val, cfile);
break;
} else if (token == EOF) {
token = next_token (&val, cfile);
parse_warn ("unexpected end of file");
break;
1996-08-27 09:40:17 +00:00
}
declaration = parse_statement (cfile, subnet -> group,
SUBNET_DECL,
(struct host_decl *)0,
declaration);
} while (1);
/* Add the subnet to the list of subnets in this shared net. */
1996-08-27 09:40:17 +00:00
if (!share -> subnets)
share -> subnets = subnet;
else {
for (t = share -> subnets;
t -> next_sibling; t = t -> next_sibling)
1996-08-27 09:40:17 +00:00
;
t -> next_sibling = subnet;
1996-08-27 09:40:17 +00:00
}
}
/* group-declaration :== RBRACE parameters declarations LBRACE */
1996-08-27 09:40:17 +00:00
void parse_group_declaration (cfile, group)
FILE *cfile;
1996-08-27 09:40:17 +00:00
struct group *group;
{
char *val;
int token;
1996-08-27 09:40:17 +00:00
struct group *g;
int declaration = 0;
g = clone_group (group, "parse_group_declaration");
1996-08-27 09:40:17 +00:00
if (!parse_lbrace (cfile))
return;
1995-11-29 07:40:04 +00:00
1996-08-27 09:40:17 +00:00
do {
token = peek_token (&val, cfile);
if (token == RBRACE) {
token = next_token (&val, cfile);
1996-08-27 09:40:17 +00:00
break;
} else if (token == EOF) {
token = next_token (&val, cfile);
parse_warn ("unexpected end of file");
break;
}
declaration = parse_statement (cfile, g, GROUP_DECL,
(struct host_decl *)0,
declaration);
1996-08-27 09:40:17 +00:00
} while (1);
1995-11-29 07:40:04 +00:00
}
/* fixed-addr-parameter :== ip-addrs-or-hostnames SEMI
ip-addrs-or-hostnames :== ip-addr-or-hostname
| ip-addrs-or-hostnames ip-addr-or-hostname */
1995-11-29 07:40:04 +00:00
struct option_cache *parse_fixed_addr_param (cfile)
1995-11-29 07:40:04 +00:00
FILE *cfile;
{
char *val;
int token;
struct expression *expr = (struct expression *)0;
struct expression *tmp;
do {
1996-08-27 09:40:17 +00:00
tmp = parse_ip_addr_or_hostname (cfile, 0);
if (expr)
expr = make_concat (expr, tmp);
else
expr = tmp;
token = peek_token (&val, cfile);
if (token == COMMA)
token = next_token (&val, cfile);
} while (token == COMMA);
1996-08-27 09:40:17 +00:00
if (!parse_semi (cfile))
return (struct option_cache *)0;
return option_cache (expr, (struct option *)0);
1995-11-29 07:40:04 +00:00
}
/* timestamp :== date
1995-11-29 07:40:04 +00:00
Timestamps are actually not used in dhcpd.conf, which is a static file,
but rather in the database file and the journal file. (Okay, actually
they're not even used there yet). */
1995-11-29 07:40:04 +00:00
1996-08-27 09:40:17 +00:00
TIME parse_timestamp (cfile)
1995-11-29 07:40:04 +00:00
FILE *cfile;
{
TIME rv;
1996-08-27 09:40:17 +00:00
rv = parse_date (cfile);
return rv;
1995-11-29 07:40:04 +00:00
}
/* lease_declaration :== LEASE ip_address LBRACE lease_parameters RBRACE
lease_parameters :== <nil>
| lease_parameter
| lease_parameters lease_parameter
lease_parameter :== STARTS date
| ENDS date
| TIMESTAMP date
| HARDWARE hardware-parameter
| UID hex_numbers SEMI
| HOSTNAME hostname SEMI
| CLIENT_HOSTNAME hostname SEMI
| CLASS identifier SEMI
| DYNAMIC_BOOTP SEMI */
struct lease *parse_lease_declaration (cfile)
1995-11-29 07:40:04 +00:00
FILE *cfile;
{
char *val;
int token;
unsigned char addr [4];
int len = sizeof addr;
int seenmask = 0;
int seenbit;
char tbuf [32];
static struct lease lease;
/* Zap the lease structure... */
memset (&lease, 0, sizeof lease);
1995-11-29 07:40:04 +00:00
/* Get the address for which the lease has been issued. */
1996-08-27 09:40:17 +00:00
if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
return (struct lease *)0;
memcpy (lease.ip_addr.iabuf, addr, len);
lease.ip_addr.len = len;
1995-11-29 07:40:04 +00:00
1996-08-27 09:40:17 +00:00
if (!parse_lbrace (cfile))
return (struct lease *)0;
1995-11-29 07:40:04 +00:00
do {
token = next_token (&val, cfile);
1996-08-27 09:40:17 +00:00
if (token == RBRACE)
1995-11-29 07:40:04 +00:00
break;
else if (token == EOF) {
parse_warn ("unexpected end of file");
break;
}
strncpy (tbuf, val, sizeof tbuf);
1995-11-29 07:40:04 +00:00
tbuf [(sizeof tbuf) - 1] = 0;
/* Parse any of the times associated with the lease. */
if (token == STARTS || token == ENDS || token == TIMESTAMP) {
TIME t;
1996-08-27 09:40:17 +00:00
t = parse_date (cfile);
1995-11-29 07:40:04 +00:00
switch (token) {
case STARTS:
seenbit = 1;
lease.starts = t;
break;
case ENDS:
seenbit = 2;
lease.ends = t;
break;
case TIMESTAMP:
seenbit = 4;
lease.timestamp = t;
break;
default:
/*NOTREACHED*/
seenbit = 0;
break;
1995-11-29 07:40:04 +00:00
}
} else {
switch (token) {
/* Colon-seperated hexadecimal octets... */
case UID:
seenbit = 8;
token = peek_token (&val, cfile);
if (token == STRING) {
token = next_token (&val, cfile);
lease.uid_len = strlen (val) + 1;
lease.uid = (unsigned char *)
malloc (lease.uid_len);
1996-09-09 07:04:29 +00:00
if (!lease.uid) {
warn ("no space for uid");
return (struct lease *)0;
}
memcpy (lease.uid, val, lease.uid_len);
} else {
lease.uid_len = 0;
lease.uid = parse_numeric_aggregate
1996-08-27 09:40:17 +00:00
(cfile, (unsigned char *)0,
&lease.uid_len, ':', 16, 8);
1996-09-09 07:04:29 +00:00
if (!lease.uid) {
warn ("no space for uid");
1996-08-27 09:40:17 +00:00
return (struct lease *)0;
1996-09-09 07:04:29 +00:00
}
if (lease.uid_len == 0) {
1996-09-09 07:04:29 +00:00
lease.uid = (unsigned char *)0;
parse_warn ("zero-length uid");
seenbit = 0;
break;
}
}
1995-11-29 07:40:04 +00:00
if (!lease.uid) {
error ("No memory for lease uid");
}
break;
case CLASS:
seenbit = 32;
token = next_token (&val, cfile);
if (!is_identifier (token)) {
if (token != SEMI)
skip_to_semi (cfile);
1996-08-27 09:40:17 +00:00
return (struct lease *)0;
1995-11-29 07:40:04 +00:00
}
/* for now, we aren't using this. */
break;
case HARDWARE:
seenbit = 64;
parse_hardware_param (cfile,
1996-08-27 09:40:17 +00:00
&lease.hardware_addr);
1995-11-29 07:40:04 +00:00
break;
case DYNAMIC_BOOTP:
seenbit = 128;
lease.flags |= BOOTP_LEASE;
break;
1997-03-06 19:29:39 +00:00
case ABANDONED:
seenbit = 256;
lease.flags |= ABANDONED_LEASE;
break;
case HOSTNAME:
seenbit = 512;
1997-10-27 20:22:40 +00:00
token = peek_token (&val, cfile);
if (token == STRING)
lease.hostname = parse_string (cfile);
else
lease.hostname =
parse_host_name (cfile);
if (!lease.hostname) {
seenbit = 0;
return (struct lease *)0;
}
break;
case CLIENT_HOSTNAME:
seenbit = 1024;
token = peek_token (&val, cfile);
if (token == STRING)
lease.client_hostname =
parse_string (cfile);
else
lease.client_hostname =
parse_host_name (cfile);
break;
1995-11-29 07:40:04 +00:00
default:
1996-08-27 09:40:17 +00:00
skip_to_semi (cfile);
seenbit = 0;
1996-08-27 09:40:17 +00:00
return (struct lease *)0;
}
1997-06-10 06:00:23 +00:00
if (token != HARDWARE && token != STRING) {
1996-08-27 09:40:17 +00:00
token = next_token (&val, cfile);
if (token != SEMI) {
parse_warn ("semicolon expected.");
skip_to_semi (cfile);
return (struct lease *)0;
}
1995-11-29 07:40:04 +00:00
}
}
if (seenmask & seenbit) {
parse_warn ("Too many %s parameters in lease %s\n",
tbuf, piaddr (lease.ip_addr));
1995-11-29 07:40:04 +00:00
} else
seenmask |= seenbit;
1996-08-27 09:40:17 +00:00
1995-11-29 07:40:04 +00:00
} while (1);
return &lease;
}
/* address-range-declaration :== ip-address ip-address SEMI
| DYNAMIC_BOOTP ip-address ip-address SEMI */
1995-11-29 07:40:04 +00:00
1996-08-27 09:40:17 +00:00
void parse_address_range (cfile, subnet)
1995-11-29 07:40:04 +00:00
FILE *cfile;
struct subnet *subnet;
1995-11-29 07:40:04 +00:00
{
struct iaddr low, high;
1995-11-29 07:40:04 +00:00
unsigned char addr [4];
int len = sizeof addr;
int token;
char *val;
int dynamic = 0;
if ((token = peek_token (&val, cfile)) == DYNAMIC_BOOTP) {
token = next_token (&val, cfile);
dynamic = 1;
}
1995-11-29 07:40:04 +00:00
/* Get the bottom address in the range... */
1996-08-27 09:40:17 +00:00
if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
return;
memcpy (low.iabuf, addr, len);
low.len = len;
1995-11-29 07:40:04 +00:00
/* Only one address? */
token = peek_token (&val, cfile);
if (token == SEMI)
high = low;
else {
1995-11-29 07:40:04 +00:00
/* Get the top address in the range... */
if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
return;
memcpy (high.iabuf, addr, len);
high.len = len;
}
1995-11-29 07:40:04 +00:00
1996-08-27 09:40:17 +00:00
token = next_token (&val, cfile);
if (token != SEMI) {
parse_warn ("semicolon expected.");
skip_to_semi (cfile);
return;
}
1995-11-29 07:40:04 +00:00
/* Create the new address range... */
new_address_range (low, high, subnet, dynamic);
1995-11-29 07:40:04 +00:00
}