Kea 2.5.9
client_class_def_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8#include <dhcp/libdhcp++.h>
9#include <dhcpsrv/cfgmgr.h>
15#include <eval/eval_context.h>
16#include <asiolink/io_address.h>
17#include <asiolink/io_error.h>
18
19#include <algorithm>
20#include <sstream>
21
22using namespace isc::data;
23using namespace isc::asiolink;
24using namespace isc::util;
25using namespace std;
26
30
31namespace isc {
32namespace dhcp {
33
34// ********************** ExpressionParser ****************************
35
36void
38 ConstElementPtr expression_cfg,
39 uint16_t family,
40 EvalContext::CheckDefined check_defined,
41 EvalContext::ParserType parser_type) {
42 if (expression_cfg->getType() != Element::string) {
43 isc_throw(DhcpConfigError, "expression ["
44 << expression_cfg->str() << "] must be a string, at ("
45 << expression_cfg->getPosition() << ")");
46 }
47
48 // Get the expression's text via getValue() as the text returned
49 // by str() enclosed in quotes.
50 std::string value;
51 expression_cfg->getValue(value);
52
53 if (parser_type == EvalContext::PARSER_STRING && value.empty()) {
54 isc_throw(DhcpConfigError, "expression can not be empty at ("
55 << expression_cfg->getPosition() << ")");
56 }
57
58 try {
59 EvalContext eval_ctx(family == AF_INET ? Option::V4 : Option::V6,
60 check_defined);
61 eval_ctx.parseString(value, parser_type);
62 expression.reset(new Expression());
63 *expression = eval_ctx.expression;
64 } catch (const std::exception& ex) {
65 // Append position if there is a failure.
67 "expression: [" << value
68 << "] error: " << ex.what() << " at ("
69 << expression_cfg->getPosition() << ")");
70 }
71}
72
73// ********************** ClientClassDefParser ****************************
74
75void
77 ConstElementPtr class_def_cfg,
78 uint16_t family,
79 bool append_error_position,
80 bool check_dependencies) {
81 // name is now mandatory, so let's deal with it first.
82 std::string name = getString(class_def_cfg, "name");
83 if (name.empty()) {
85 "not empty parameter 'name' is required "
86 << getPosition("name", class_def_cfg) << ")");
87 }
88
90
91 // Let's try to parse the template-test expression
92 bool is_template = false;
93
94 // Parse matching expression
95 ExpressionPtr match_expr;
96 ConstElementPtr test_cfg = class_def_cfg->get("test");
97 ConstElementPtr template_test_cfg = class_def_cfg->get("template-test");
98 if (test_cfg && template_test_cfg) {
99 isc_throw(DhcpConfigError, "can not use both 'test' and 'template-test' ("
100 << test_cfg->getPosition() << ") and ("
101 << template_test_cfg->getPosition() << ")");
102 }
103 std::string test;
104 bool depend_on_known = false;
106 if (template_test_cfg) {
107 test_cfg = template_test_cfg;
108 parser_type = EvalContext::PARSER_STRING;
109 is_template = true;
110 } else {
111 check_defined = [&class_dictionary, &depend_on_known, check_dependencies](const ClientClass& cclass) {
112 return (!check_dependencies || isClientClassDefined(class_dictionary, depend_on_known, cclass));
113 };
114 }
115
116 if (test_cfg) {
117 ExpressionParser parser;
118 parser.parse(match_expr, test_cfg, family, check_defined, parser_type);
119 test = test_cfg->stringValue();
120 }
121
122 // Parse option def
123 CfgOptionDefPtr defs(new CfgOptionDef());
124 ConstElementPtr option_defs = class_def_cfg->get("option-def");
125 if (option_defs) {
126 // Apply defaults
128 family == AF_INET ?
131
132 OptionDefParser parser(family);
133 for (auto const& option_def : option_defs->listValue()) {
134 OptionDefinitionPtr def = parser.parse(option_def);
135
136 // Verify if the definition is for an option which is in a deferred
137 // processing list.
138 if (!LibDHCP::shouldDeferOptionUnpack(def->getOptionSpaceName(),
139 def->getCode())) {
141 "Not allowed option definition for code '"
142 << def->getCode() << "' in space '"
143 << def->getOptionSpaceName() << "' at ("
144 << option_def->getPosition() << ")");
145 }
146 try {
147 defs->add(def);
148 } catch (const std::exception& ex) {
149 // Sanity check: it should never happen
150 isc_throw(DhcpConfigError, ex.what() << " ("
151 << option_def->getPosition() << ")");
152 }
153 }
154 }
155
156 // Parse option data
157 CfgOptionPtr options(new CfgOption());
158 ConstElementPtr option_data = class_def_cfg->get("option-data");
159 if (option_data) {
160 auto opts_parser = createOptionDataListParser(family, defs);
161 opts_parser->parse(options, option_data);
162 }
163
164 // Parse user context
165 ConstElementPtr user_context = class_def_cfg->get("user-context");
166 if (user_context) {
167 if (user_context->getType() != Element::map) {
168 isc_throw(isc::dhcp::DhcpConfigError, "User context has to be a map ("
169 << user_context->getPosition() << ")");
170 }
171 }
172
173 // Let's try to parse the only-if-required flag
174 bool required = false;
175 if (class_def_cfg->contains("only-if-required")) {
176 required = getBoolean(class_def_cfg, "only-if-required");
177 }
178
179 // Let's try to parse the next-server field
180 IOAddress next_server("0.0.0.0");
181 if (class_def_cfg->contains("next-server")) {
182 std::string next_server_txt = getString(class_def_cfg, "next-server");
183 try {
184 next_server = IOAddress(next_server_txt);
185 } catch (const IOError& ex) {
187 "Invalid next-server value specified: '"
188 << next_server_txt << "' ("
189 << getPosition("next-server", class_def_cfg) << ")");
190 }
191
192 if (next_server.getFamily() != AF_INET) {
193 isc_throw(DhcpConfigError, "Invalid next-server value: '"
194 << next_server_txt << "', must be IPv4 address ("
195 << getPosition("next-server", class_def_cfg) << ")");
196 }
197
198 if (next_server.isV4Bcast()) {
199 isc_throw(DhcpConfigError, "Invalid next-server value: '"
200 << next_server_txt << "', must not be a broadcast ("
201 << getPosition("next-server", class_def_cfg) << ")");
202 }
203 }
204
205 // Let's try to parse server-hostname
206 std::string sname;
207 if (class_def_cfg->contains("server-hostname")) {
208 sname = getString(class_def_cfg, "server-hostname");
209
210 if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
211 isc_throw(DhcpConfigError, "server-hostname must be at most "
212 << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
213 << sname.length() << " ("
214 << getPosition("server-hostname", class_def_cfg) << ")");
215 }
216 }
217
218 // Let's try to parse boot-file-name
219 std::string filename;
220 if (class_def_cfg->contains("boot-file-name")) {
221 filename = getString(class_def_cfg, "boot-file-name");
222
223 if (filename.length() > Pkt4::MAX_FILE_LEN) {
224 isc_throw(DhcpConfigError, "boot-file-name must be at most "
225 << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
226 << filename.length() << " ("
227 << getPosition("boot-file-name", class_def_cfg) << ")");
228 }
229 }
230
231 Optional<uint32_t> offer_lft;
232 if (class_def_cfg->contains("offer-lifetime")) {
233 auto value = getInteger(class_def_cfg, "offer-lifetime");
234 if (value < 0) {
235 isc_throw(DhcpConfigError, "the value of offer-lifetime '"
236 << value << "' must be a positive number ("
237 << getPosition("offer-lifetime", class_def_cfg) << ")");
238 }
239
240 offer_lft = value;
241 }
242
243 // Parse valid lifetime triplet.
244 Triplet<uint32_t> valid_lft = parseIntTriplet(class_def_cfg, "valid-lifetime");
245
246 Triplet<uint32_t> preferred_lft;
247 if (family != AF_INET) {
248 // Parse preferred lifetime triplet.
249 preferred_lft = parseIntTriplet(class_def_cfg, "preferred-lifetime");
250 }
251
252 // Sanity checks on built-in classes
253 for (auto const& bn : builtinNames) {
254 if (name == bn) {
255 if (required) {
256 isc_throw(DhcpConfigError, "built-in class '" << name
257 << "' only-if-required flag must be false");
258 }
259 if (!test.empty()) {
260 isc_throw(DhcpConfigError, "built-in class '" << name
261 << "' test expression must be empty");
262 }
263 }
264 }
265
266 // Sanity checks on DROP
267 if (name == "DROP") {
268 if (required) {
269 isc_throw(DhcpConfigError, "special class '" << name
270 << "' only-if-required flag must be false");
271 }
272 // depend_on_known is now allowed
273 }
274
275 // Add the client class definition
276 try {
277 class_dictionary->addClass(name, match_expr, test, required,
278 depend_on_known, options, defs,
279 user_context, next_server, sname, filename,
280 valid_lft, preferred_lft, is_template, offer_lft);
281 } catch (const std::exception& ex) {
282 std::ostringstream s;
283 s << "Can't add class: " << ex.what();
284 // Append position of the error in JSON string if required.
285 if (append_error_position) {
286 s << " (" << class_def_cfg->getPosition() << ")";
287 }
288 isc_throw(DhcpConfigError, s.str());
289 }
290}
291
292void
294 const uint16_t family) {
295 // Make sure that the client class definition is stored in a map.
296 if (!class_def_cfg || (class_def_cfg->getType() != Element::map)) {
297 isc_throw(DhcpConfigError, "client class definition is not a map");
298 }
299
300 // Common v4 and v6 parameters supported for the client class.
301 static std::set<std::string> supported_params = { "name",
302 "test",
303 "option-data",
304 "user-context",
305 "only-if-required",
306 "valid-lifetime",
307 "min-valid-lifetime",
308 "max-valid-lifetime",
309 "template-test"};
310
311 // The v4 client class supports additional parameters.
312 static std::set<std::string> supported_params_v4 = { "option-def",
313 "next-server",
314 "server-hostname",
315 "boot-file-name" };
316
317 // The v6 client class supports additional parameters.
318 static std::set<std::string> supported_params_v6 = { "preferred-lifetime",
319 "min-preferred-lifetime",
320 "max-preferred-lifetime" };
321
322 // Iterate over the specified parameters and check if they are all supported.
323 for (auto const& name_value_pair : class_def_cfg->mapValue()) {
324 if ((supported_params.count(name_value_pair.first) > 0) ||
325 ((family == AF_INET) && (supported_params_v4.count(name_value_pair.first) > 0)) ||
326 ((family != AF_INET) && (supported_params_v6.count(name_value_pair.first) > 0))) {
327 continue;
328 } else {
329 isc_throw(DhcpConfigError, "unsupported client class parameter '"
330 << name_value_pair.first << "'");
331 }
332 }
333}
334
335boost::shared_ptr<OptionDataListParser>
337 CfgOptionDefPtr cfg_option_def) const {
338 auto parser = boost::make_shared<OptionDataListParser>(address_family, cfg_option_def);
339 return (parser);
340}
341
342// ****************** ClientClassDefListParser ************************
343
346 uint16_t family, bool check_dependencies) {
348 for (auto const& client_class_def : client_class_def_list->listValue()) {
350 parser.parse(dictionary, client_class_def, family, true, check_dependencies);
351 }
352 return (dictionary);
353}
354
355} // end of namespace isc::dhcp
356} // end of namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static size_t setListDefaults(isc::data::ConstElementPtr list, const SimpleDefaults &default_values)
Sets the default values for all entries in a list.
static const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent)
Utility method that returns position of an element.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
const isc::util::Triplet< uint32_t > parseIntTriplet(const data::ConstElementPtr &scope, const std::string &name)
Parses an integer triplet.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
static int64_t getInteger(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer parameter from a scope.
Represents option definitions used by the DHCP server.
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:352
ClientClassDictionaryPtr parse(isc::data::ConstElementPtr class_def_list, uint16_t family, bool check_dependencies=true)
Parse configuration entries.
Parser for a single client class definition.
virtual boost::shared_ptr< OptionDataListParser > createOptionDataListParser(const uint16_t address_family, CfgOptionDefPtr cfg_option_def) const
Returns an instance of the OptionDataListParser to be used in parsing the option-data structure.
void parse(ClientClassDictionaryPtr &class_dictionary, isc::data::ConstElementPtr client_class_def, uint16_t family, bool append_error_position=true, bool check_dependencies=true)
Parses an entry that describes single client class definition.
void checkParametersSupported(const isc::data::ConstElementPtr &class_def_cfg, const uint16_t family)
Iterates over class parameters and checks if they are supported.
Maintains a list of ClientClassDef's.
To be removed. Please use ConfigError instead.
Parser for a logical expression.
void parse(ExpressionPtr &expression, isc::data::ConstElementPtr expression_cfg, uint16_t family, isc::eval::EvalContext::CheckDefined check_defined=isc::eval::EvalContext::acceptAll, isc::eval::EvalContext::ParserType parser_type=isc::eval::EvalContext::PARSER_BOOL)
Parses an expression configuration element into an Expression.
static bool shouldDeferOptionUnpack(const std::string &space, const uint16_t code)
Checks if an option unpacking has to be deferred.
Definition: libdhcp++.cc:280
Parser for a single option definition.
Definition: dhcp_parsers.h:228
OptionDefinitionPtr parse(isc::data::ConstElementPtr option_def)
Parses an entry that describes single option definition.
Definition: dhcp_parsers.cc:98
static const size_t MAX_SNAME_LEN
length of the SNAME field in DHCPv4 message
Definition: pkt4.h:44
static const size_t MAX_FILE_LEN
length of the FILE field in DHCPv4 message
Definition: pkt4.h:47
static const isc::data::SimpleDefaults OPTION4_DEF_DEFAULTS
This table defines default values for option definitions in DHCPv4.
static const isc::data::SimpleDefaults OPTION6_DEF_DEFAULTS
This table defines default values for option definitions in DHCPv6.
Evaluation context, an interface to the expression evaluation.
Definition: eval_context.h:34
std::function< bool(const ClientClass &)> CheckDefined
Type of the check defined function.
Definition: eval_context.h:44
bool parseString(const std::string &str, ParserType type=PARSER_BOOL)
Run the parser on the string specified.
Definition: eval_context.cc:37
ParserType
Specifies what type of expression the parser is expected to see.
Definition: eval_context.h:38
@ PARSER_BOOL
expression is expected to evaluate to bool
Definition: eval_context.h:39
@ PARSER_STRING
expression is expected to evaluate to string
Definition: eval_context.h:40
isc::dhcp::Expression expression
Parsed expression (output tokens are stored here)
Definition: eval_context.h:67
static bool acceptAll(const ClientClass &client_class)
Accept all client class names.
Definition: eval_context.cc:32
A template representing an optional value.
Definition: optional.h:36
This template specifies a parameter value.
Definition: triplet.h:37
Defines classes for storing client class definitions.
Parsers for client class definitions.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:803
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
boost::shared_ptr< Expression > ExpressionPtr
Definition: token.h:30
boost::shared_ptr< ClientClassDictionary > ClientClassDictionaryPtr
Defines a pointer to a ClientClassDictionary.
bool isClientClassDefined(ClientClassDictionaryPtr &class_dictionary, bool &depend_on_known, const ClientClass &client_class)
Check if a client class name is already defined, i.e.
std::vector< TokenPtr > Expression
This is a structure that holds an expression converted to RPN.
Definition: token.h:28
std::list< std::string > builtinNames
List of classes for which test expressions cannot be defined.
Defines the logger used by the top-level component of kea-lfc.