Home | History | Annotate | Line # | Download | only in keama
      1 /*	$NetBSD: keama.h,v 1.3 2022/04/03 01:10:59 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2017-2022 Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     16  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  *
     18  *   Internet Systems Consortium, Inc.
     19  *   PO Box 360
     20  *   Newmarket, NH 03857 USA
     21  *   <info (at) isc.org>
     22  *   https://www.isc.org/
     23  *
     24  */
     25 
     26 #ifndef EOL
     27 #define EOL '\n'
     28 #endif
     29 
     30 #include "data.h"
     31 #include "dhctoken.h"
     32 
     33 #include <time.h>
     34 
     35 /* Resolution of FQDNs into IPv4 addresses */
     36 enum resolve {
     37 	perform = 0,	/* resolve */
     38 	fatal,		/* raise a fatal error */
     39 	pass		/* pass the string wth a warning */
     40 };
     41 
     42 extern enum resolve resolve;
     43 
     44 /* From includes/dhcp.h */
     45 
     46 #define HTYPE_ETHER	1
     47 #define HTYPE_IEEE802	6
     48 #define HTYPE_FDDI	8
     49 
     50 #define DHO_DHCP_SERVER_IDENTIFIER		54
     51 #define DHO_VENDOR_CLASS_IDENTIFIER		60
     52 #define DHO_USER_CLASS				77
     53 #define DHO_VIVSO_SUBOPTIONS			125
     54 
     55 /* From includes/dhcp6.h */
     56 #define D6O_VENDOR_OPTS				17
     57 #define MAX_V6RELAY_HOPS			32
     58 
     59 /* From includes/dhcpd.h */
     60 
     61 extern int local_family;
     62 
     63 /* A parsing context. */
     64 
     65 struct parse {
     66 	int lexline;
     67 	int lexchar;
     68 	char *token_line;
     69 	char *prev_line;
     70 	char *cur_line;
     71 	const char *tlname;
     72 	int eol_token;
     73 
     74 	/*
     75 	 * In order to give nice output when we have a parsing error
     76 	 * in our file, we keep track of where we are in the line so
     77 	 * that we can show the user.
     78 	 *
     79 	 * We need to keep track of two lines, because we can look
     80 	 * ahead, via the "peek" function, to the next line sometimes.
     81 	 *
     82 	 * The "line1" and "line2" variables act as buffers for this
     83 	 * information. The "lpos" variable tells us where we are in the
     84 	 * line.
     85 	 *
     86 	 * When we "put back" a character from the parsing context, we
     87 	 * do not want to have the character appear twice in the error
     88 	 * output. So, we set a flag, the "ugflag", which the
     89 	 * get_char() function uses to check for this condition.
     90 	 */
     91 	char line1[81];
     92 	char line2[81];
     93 	int lpos;
     94 	int line;
     95 	int tlpos;
     96 	int tline;
     97 	enum dhcp_token token;
     98 	int ugflag;
     99 	char *tval;
    100 	int tlen;
    101 	char tokbuf[1500];
    102 
    103 	int warnings_occurred;
    104 	int file;
    105 	char *inbuf;
    106 	size_t bufix, buflen;
    107 	size_t bufsiz;
    108 
    109 	/*
    110 	 * Additions for the Kea Migration Assistant.
    111 	 */
    112 	struct element **stack;
    113 	size_t stack_size;
    114 	size_t stack_top;
    115 	size_t issue_counter;
    116 
    117 	/* don't save below this */
    118 	struct comments comments;
    119 
    120 	/* TAILQ_NEXT(self) is the saved state */
    121 	TAILQ_ENTRY(parse) next;
    122 
    123 };
    124 
    125 extern TAILQ_HEAD(parses, parse) parses;
    126 
    127 #define PARAMETER	0
    128 #define TOPLEVEL	1
    129 #define	ROOT_GROUP	2
    130 #define HOST_DECL	3
    131 #define SHARED_NET_DECL	4
    132 #define SUBNET_DECL	5
    133 #define CLASS_DECL	6
    134 #define	GROUP_DECL	7
    135 #define POOL_DECL	8
    136 
    137 /* Used as an argument to parse_class_decl() */
    138 #define CLASS_TYPE_VENDOR	0
    139 #define CLASS_TYPE_USER		1
    140 #define CLASS_TYPE_CLASS	2
    141 #define CLASS_TYPE_SUBCLASS	3
    142 
    143 #define CLASS_DECL_DELETED	1
    144 #define CLASS_DECL_DYNAMIC	2
    145 #define CLASS_DECL_STATIC	4
    146 #define CLASS_DECL_SUBCLASS	8
    147 
    148 /* Hardware buffer size */
    149 #define HARDWARE_ADDR_LEN 20
    150 
    151 /* Expression context */
    152 
    153 enum expression_context {
    154 	context_any, /* indefinite */
    155 	context_boolean,
    156 	context_data,
    157 	context_numeric,
    158 	context_dns,
    159 	context_data_or_numeric, /* indefinite */
    160 	context_function
    161 };
    162 
    163 /* Statements */
    164 
    165 enum statement_op {
    166 	null_statement,
    167 	if_statement,
    168 	add_statement,
    169 	eval_statement,
    170 	break_statement,
    171 	default_option_statement,
    172 	supersede_option_statement,
    173 	append_option_statement,
    174 	prepend_option_statement,
    175 	send_option_statement,
    176 	statements_statement,
    177 	on_statement,
    178 	switch_statement,
    179 	case_statement,
    180 	default_statement,
    181 	set_statement,
    182 	unset_statement,
    183 	let_statement,
    184 	define_statement,
    185 	log_statement,
    186 	return_statement,
    187 	execute_statement,
    188 	vendor_opt_statement
    189 };
    190 
    191 /* Expression tree structure. */
    192 
    193 enum expr_op {
    194 	expr_none,
    195 	expr_match,
    196 	expr_check,
    197 	expr_equal,
    198 	expr_substring,
    199 	expr_suffix,
    200 	expr_concat,
    201 	expr_host_lookup,
    202 	expr_and,
    203 	expr_or,
    204 	expr_not,
    205 	expr_option,
    206 	expr_hardware,
    207 	expr_hw_type,		/* derived from expr_hardware */
    208 	expr_hw_address,	/* derived from expr_hardware */
    209 	expr_packet,
    210 	expr_const_data,
    211 	expr_extract_int8,
    212 	expr_extract_int16,
    213 	expr_extract_int32,
    214 	expr_encode_int8,
    215 	expr_encode_int16,
    216 	expr_encode_int32,
    217 	expr_const_int,
    218 	expr_exists,
    219 	expr_encapsulate,
    220 	expr_known,
    221 	expr_reverse,
    222 	expr_leased_address,
    223 	expr_binary_to_ascii,
    224 	expr_config_option,
    225 	expr_host_decl_name,
    226 	expr_pick_first_value,
    227  	expr_lease_time,
    228  	expr_dns_transaction,
    229 	expr_static,
    230 	expr_ns_add,
    231  	expr_ns_delete,
    232  	expr_ns_exists,
    233  	expr_ns_not_exists,
    234 	expr_not_equal,
    235 	expr_null,
    236 	expr_variable_exists,
    237 	expr_variable_reference,
    238 	expr_filename,
    239  	expr_sname,
    240 	expr_arg,
    241 	expr_funcall,
    242 	expr_function,
    243 	expr_add,
    244 	expr_subtract,
    245 	expr_multiply,
    246 	expr_divide,
    247 	expr_remainder,
    248 	expr_binary_and,
    249 	expr_binary_or,
    250 	expr_binary_xor,
    251 	expr_client_state,
    252 	expr_ucase,
    253 	expr_lcase,
    254 	expr_regex_match,
    255 	expr_iregex_match,
    256 	expr_gethostname,
    257 	expr_v6relay,
    258 	expr_concat_dclist
    259 };
    260 
    261 /* options */
    262 
    263 enum option_status {
    264 	kea_unknown = 0,	/* known only by ISC DHCP */
    265 	isc_dhcp_unknown = 1,	/* known only by Kea */
    266 	known = 2,		/* known by both ISC DHCP and Kea */
    267 	special = 3,		/* requires special processing */
    268 	dynamic = 4		/* dynamic entry */
    269 };
    270 
    271 struct option_def {			/* ISC DHCP option definition */
    272 	const char *name;		/* option name */
    273 	const char *format;		/* format string */
    274 	const char *space;		/* space (aka universe) */
    275 	unsigned code;			/* code point */
    276 	enum option_status status;	/* status */
    277 };
    278 
    279 struct space_def {			/* ISC DHCP space definition */
    280 	const char *old;		/* ISC DHCP space name */
    281 	const char *name;		/* Kea space name */
    282 	enum option_status status;	/* status */
    283 };
    284 
    285 struct space {
    286 	const char *old;		/* ISC DHCP space name */
    287 	const char *name;		/* Kea space name */
    288 	enum option_status status;	/* status */
    289 	struct element *vendor;		/* vendor option */
    290 	TAILQ_ENTRY(space) next;	/* next space */
    291 };
    292 
    293 struct option {
    294 	const char *old;		/* ISC DHCP option name */
    295 	const char *name;		/* Kea option name */
    296 	const char *format;		/* ISC DHCP format string */
    297 	const struct space *space;	/* space (aka universe) */
    298 	unsigned code;			/* code point */
    299 	enum option_status status;	/* status */
    300 	TAILQ_ENTRY(option) next;	/* next option */
    301 };
    302 
    303 /* Kea parse tools */
    304 void stackPush(struct parse *cfile, struct element *elem);
    305 
    306 /* From command line */
    307 extern char *hook_library_path;
    308 extern isc_boolean_t use_isc_lifetimes;
    309 extern isc_boolean_t global_hr;
    310 
    311 /* From common/parse.c */
    312 void parse_error(struct parse *, const char *, ...)
    313 	__attribute__((__format__(__printf__,2,3)))
    314 	__attribute__((noreturn));
    315 
    316 /* conflex.c */
    317 struct parse *new_parse(int, char *, size_t, const char *, int);
    318 void end_parse(struct parse *);
    319 void save_parse_state(struct parse *);
    320 void restore_parse_state(struct parse *);
    321 enum dhcp_token next_token(const char **, unsigned *, struct parse *);
    322 enum dhcp_token peek_token(const char **, unsigned *, struct parse *);
    323 enum dhcp_token next_raw_token(const char **, unsigned *, struct parse *);
    324 enum dhcp_token peek_raw_token(const char **, unsigned *, struct parse *);
    325 /*
    326  * Use skip_token when we are skipping a token we have previously
    327  * used peek_token on as we know what the result will be in this case.
    328  */
    329 #define skip_token(a,b,c) ((void) next_token((a),(b),(c)))
    330 
    331 /* confparse.c */
    332 size_t conf_file_parse(struct parse *);
    333 void read_conf_file(struct parse *, const char *, int);
    334 size_t conf_file_subparse(struct parse *, int);
    335 isc_boolean_t parse_statement(struct parse *, int, isc_boolean_t);
    336 void get_permit(struct parse *, struct element *);
    337 void parse_pool_statement(struct parse *, int);
    338 void parse_lbrace(struct parse *);
    339 void parse_host_declaration(struct parse *);
    340 void parse_class_declaration(struct parse *, int);
    341 void parse_shared_net_declaration(struct parse *);
    342 void parse_subnet_declaration(struct parse *);
    343 void parse_subnet6_declaration(struct parse *);
    344 void parse_group_declaration(struct parse *);
    345 void close_group(struct parse *, struct element *);
    346 struct element *parse_fixed_addr_param(struct parse *, enum dhcp_token);
    347 void parse_address_range(struct parse *, int, size_t);
    348 void parse_address_range6(struct parse *, int, size_t);
    349 void parse_prefix6(struct parse *, int, size_t);
    350 void parse_fixed_prefix6(struct parse *, size_t);
    351 void parse_pool6_statement(struct parse *, int);
    352 struct element *parse_allow_deny(struct parse *, int);
    353 void parse_server_duid_conf(struct parse *);
    354 void parse_directive(struct parse *);
    355 void parse_option_space_dir(struct parse *);
    356 void parse_option_code_dir(struct parse *, struct option *);
    357 void parse_option_status_dir(struct parse *, struct option *, enum dhcp_token);
    358 void parse_option_local_dir(struct parse *, struct option *);
    359 void parse_option_define_dir(struct parse *, struct option *);
    360 
    361 /* parse.c */
    362 void skip_to_semi(struct parse *);
    363 void skip_to_rbrace(struct parse *, int);
    364 void parse_semi(struct parse *);
    365 void parse_string(struct parse *, char **, unsigned *);
    366 struct string *parse_host_name(struct parse *);
    367 struct string *parse_ip_addr_or_hostname(struct parse *, isc_boolean_t);
    368 struct string *parse_ip_addr(struct parse *);
    369 struct string *parse_ip6_addr(struct parse *);
    370 struct string *parse_ip6_addr_txt(struct parse *);
    371 struct element *parse_hardware_param(struct parse *);
    372 void parse_lease_time(struct parse *, time_t *);
    373 struct string *parse_numeric_aggregate(struct parse *,
    374 				       unsigned char *, unsigned *,
    375 				       int, int, unsigned);
    376 void convert_num(struct parse *, unsigned char *, const char *,
    377 		 int, unsigned);
    378 struct option *parse_option_name(struct parse *, isc_boolean_t,
    379 				 isc_boolean_t *);
    380 void parse_option_space_decl(struct parse *);
    381 void parse_option_code_definition(struct parse *, struct option *);
    382 void parse_vendor_code_definition(struct parse *, struct option *);
    383 struct string *convert_format(const char *, isc_boolean_t *, isc_boolean_t *);
    384 struct string *parse_base64(struct parse *);
    385 struct string *parse_cshl(struct parse *);
    386 struct string *parse_hexa(struct parse *);
    387 isc_boolean_t parse_executable_statements(struct element *,
    388 					  struct parse *, isc_boolean_t *,
    389 					  enum expression_context);
    390 isc_boolean_t parse_executable_statement(struct element *,
    391 					 struct parse *, isc_boolean_t *,
    392 					 enum expression_context,
    393 					 isc_boolean_t);
    394 isc_boolean_t parse_zone(struct element *, struct parse *);
    395 isc_boolean_t parse_key(struct element *, struct parse *);
    396 isc_boolean_t parse_on_statement(struct element *, struct parse *,
    397 				 isc_boolean_t *);
    398 isc_boolean_t parse_switch_statement(struct element *, struct parse *,
    399 				     isc_boolean_t *);
    400 isc_boolean_t parse_case_statement(struct element *, struct parse *,
    401 				   isc_boolean_t *, enum expression_context);
    402 isc_boolean_t parse_if_statement(struct element *, struct parse *,
    403 				 isc_boolean_t *);
    404 isc_boolean_t parse_boolean_expression(struct element *, struct parse *,
    405 				       isc_boolean_t *);
    406 /* currently unused */
    407 isc_boolean_t parse_boolean(struct parse *);
    408 isc_boolean_t parse_data_expression(struct element *, struct parse *,
    409 				    isc_boolean_t *);
    410 isc_boolean_t numeric_expression(struct element *, struct parse *,
    411 				 isc_boolean_t *);
    412 isc_boolean_t parse_non_binary(struct element *, struct parse *,
    413 			       isc_boolean_t *, enum expression_context);
    414 isc_boolean_t parse_expression(struct element *, struct parse *,
    415 			       isc_boolean_t *, enum expression_context,
    416 			       struct element *, enum expr_op);
    417 struct string *escape_option_string(unsigned, const char *,
    418 				   isc_boolean_t *, isc_boolean_t *);
    419 isc_boolean_t parse_option_data(struct element *, struct parse *,
    420 				struct option *);
    421 isc_boolean_t parse_option_binary(struct element *, struct parse *,
    422 				  struct option *, isc_boolean_t);
    423 struct string * parse_option_textbin(struct parse *, struct option *);
    424 isc_boolean_t parse_option_statement(struct element *, struct parse *,
    425 				     struct option *, enum statement_op);
    426 isc_boolean_t parse_config_data(struct element *, struct parse *,
    427 				struct option *);
    428 isc_boolean_t parse_config_statement(struct element *, struct parse *,
    429 				     struct option *, enum statement_op);
    430 struct string *parse_option_token(struct parse *, const char *,
    431 				  isc_boolean_t *, isc_boolean_t *,
    432 				  isc_boolean_t *);
    433 struct string *parse_option_token_binary(struct parse *, const char *);
    434 struct string *parse_domain_list(struct parse *, isc_boolean_t);
    435 isc_boolean_t is_boolean_expression(struct element *);
    436 isc_boolean_t is_data_expression(struct element *);
    437 isc_boolean_t is_numeric_expression(struct element *);
    438 int expr_precedence(enum expr_op, struct element *);
    439 
    440 /* options.c */
    441 void spaces_init(void);
    442 void options_init(void);
    443 struct space *space_lookup(const char *);
    444 struct option *option_lookup_name(const char *, const char *);
    445 struct option *kea_lookup_name(const char *, const char *);
    446 struct option *option_lookup_code(const char *, unsigned);
    447 void push_space(struct space *);
    448 void push_option(struct option *);
    449 void add_option_data(struct element *, struct element *);
    450 void merge_option_data(struct element *, struct element *);
    451 struct comments *get_config_comments(unsigned);
    452 const char *display_status(enum option_status);
    453 
    454 /* json.c */
    455 struct element *json_parse(struct parse *);
    456 struct element *json_list_parse(struct parse *);
    457 struct element *json_map_parse(struct parse *);
    458 
    459 /* print.c */
    460 const char *print_expression(struct element *, isc_boolean_t *);
    461 const char *print_boolean_expression(struct element *, isc_boolean_t *);
    462 const char *print_data_expression(struct element *, isc_boolean_t *);
    463 const char *print_numeric_expression(struct element *, isc_boolean_t *);
    464 
    465 /* reduce.c */
    466 struct element *reduce_boolean_expression(struct element *);
    467 struct element *reduce_data_expression(struct element *);
    468 struct element *reduce_numeric_expression(struct element *);
    469 
    470 /* eval */
    471 struct element *eval_expression(struct element *, isc_boolean_t *);
    472 struct element *eval_boolean_expression(struct element *, isc_boolean_t *);
    473 struct element *eval_data_expression(struct element *, isc_boolean_t *);
    474 struct element *eval_numeric_expression(struct element *, isc_boolean_t *);
    475