Kea 3.3.4
client_class_def_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2026 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>
11#include <dhcpsrv/dhcpsrv_log.h>
16#include <eval/eval_context.h>
17#include <asiolink/io_address.h>
18#include <asiolink/io_error.h>
19
20#include <algorithm>
21#include <sstream>
22
23using namespace isc::data;
24using namespace isc::asiolink;
25using namespace isc::util;
26using namespace std;
27
31
32namespace isc {
33namespace dhcp {
34
35// ********************** ExpressionParser ****************************
36
37void
39 ConstElementPtr expression_cfg,
40 uint16_t family,
41 EvalContext::CheckDefined check_defined,
42 EvalContext::ParserType parser_type) {
43 if (expression_cfg->getType() != Element::string) {
44 isc_throw(DhcpConfigError, "expression ["
45 << expression_cfg->str() << "] must be a string, at ("
46 << expression_cfg->getPosition() << ")");
47 }
48
49 // Get the expression's text via getValue() as the text returned
50 // by str() enclosed in quotes.
51 std::string value;
52 expression_cfg->getValue(value);
53
54 if (parser_type == EvalContext::PARSER_STRING && value.empty()) {
55 isc_throw(DhcpConfigError, "expression can not be empty at ("
56 << expression_cfg->getPosition() << ")");
57 }
58
59 try {
60 EvalContext eval_ctx(family == AF_INET ? Option::V4 : Option::V6,
61 check_defined);
62 eval_ctx.parseString(value, parser_type);
63 expression.reset(new Expression());
64 *expression = eval_ctx.expression_;
65 } catch (const std::exception& ex) {
66 // Append position if there is a failure.
68 "expression: [" << value
69 << "] error: " << ex.what() << " at ("
70 << expression_cfg->getPosition() << ")");
71 }
72}
73
74// ********************** ClientClassDefParser ****************************
75
76void
78 ConstElementPtr class_def_cfg,
79 uint16_t family,
80 bool append_error_position,
81 bool check_dependencies) {
82 // name is now mandatory, so let's deal with it first.
83 std::string name = getString(class_def_cfg, "name");
84 if (name.empty()) {
86 "not empty parameter 'name' is required "
87 << getPosition("name", class_def_cfg) << ")");
88 }
89 // Note that the escape character is not problematic so is not escaped.
90 std::string escaped = ClientClasses::escape(name, false);
91 if (escaped != name) {
92 // Two possible long term behaviors: reject the name or sanitize it.
94 .arg(name)
95 .arg(escaped);
96 }
97
99
100 // Let's try to parse the template-test expression
101 bool is_template = false;
102
103 // Parse matching expression
104 ExpressionPtr match_expr;
105 ConstElementPtr test_cfg = class_def_cfg->get("test");
106 ConstElementPtr template_test_cfg = class_def_cfg->get("template-test");
107 if (test_cfg && template_test_cfg) {
108 isc_throw(DhcpConfigError, "can not use both 'test' and 'template-test' ("
109 << test_cfg->getPosition() << ") and ("
110 << template_test_cfg->getPosition() << ")");
111 }
112 std::string test;
113 bool depend_on_known = false;
115 if (template_test_cfg) {
116 test_cfg = template_test_cfg;
117 parser_type = EvalContext::PARSER_STRING;
118 is_template = true;
119 // We need to check for dependency on KNOWN, other than that we do not require
120 // classes in a template expression to be defined. This permits them to
121 // reference spawned classes. In other words, one template can depend on
122 // membership in the spawn of another template.
123 check_defined = [&depend_on_known](const ClientClass& cclass) {
124 // Check direct dependency on [UN]KNOWN
125 if ((cclass == "KNOWN") || (cclass == "UNKNOWN")) {
126 depend_on_known = true;
127 }
128
129 return (true);
130 };
131 } else {
132 check_defined =
133 [&class_dictionary, &depend_on_known, check_dependencies](const ClientClass& cclass) {
134 return (!check_dependencies || isClientClassDefined(class_dictionary,
135 depend_on_known, cclass));
136 };
137 }
138
139 if (test_cfg) {
140 ExpressionParser parser;
141 parser.parse(match_expr, test_cfg, family, check_defined, parser_type);
142 test = test_cfg->stringValue();
143 }
144
145 // Parse option def
146 CfgOptionDefPtr defs(new CfgOptionDef());
147 ConstElementPtr option_defs = class_def_cfg->get("option-def");
148 if (option_defs) {
149 // Apply defaults
151 family == AF_INET ?
154
155 OptionDefParser parser(family);
156 for (auto const& option_def : option_defs->listValue()) {
157 OptionDefinitionPtr def = parser.parse(option_def);
158
159 // Verify if the definition is for an option which is in a deferred
160 // processing list.
161 if (!LibDHCP::shouldDeferOptionUnpack(def->getOptionSpaceName(),
162 def->getCode())) {
164 "Not allowed option definition for code '"
165 << def->getCode() << "' in space '"
166 << def->getOptionSpaceName() << "' at ("
167 << option_def->getPosition() << ")");
168 }
169 try {
170 defs->add(def);
171 } catch (const std::exception& ex) {
172 // Sanity check: it should never happen
173 isc_throw(DhcpConfigError, ex.what() << " ("
174 << option_def->getPosition() << ")");
175 }
176 }
177 }
178
179 // Parse option data
180 CfgOptionPtr options(new CfgOption());
181 ConstElementPtr option_data = class_def_cfg->get("option-data");
182 if (option_data) {
183 auto opts_parser = createOptionDataListParser(family, defs);
184 opts_parser->parse(options, option_data);
185 }
186
187 // Parse user context
188 ConstElementPtr user_context = class_def_cfg->get("user-context");
189 if (user_context) {
190 if (user_context->getType() != Element::map) {
191 isc_throw(isc::dhcp::DhcpConfigError, "User context has to be a map ("
192 << user_context->getPosition() << ")");
193 }
194 }
195
196 // Let's try to parse the only-in-additional-list flag
197 auto additional_elem = class_def_cfg->get("only-in-additional-list");
198 bool additional = false;
199 if (additional_elem) {
200 if (additional_elem->getType() == Element::boolean) {
201 additional = additional_elem->boolValue();
202 } else {
204 "'only-in-additional-list' must be boolean"
205 << additional_elem->getPosition());
206 }
207 }
208
209 // Let's try to parse the next-server field
210 IOAddress next_server("0.0.0.0");
211 if (class_def_cfg->contains("next-server")) {
212 std::string next_server_txt = getString(class_def_cfg, "next-server");
213 try {
214 next_server = IOAddress(next_server_txt);
215 } catch (const IOError& ex) {
217 "Invalid next-server value specified: '"
218 << next_server_txt << "' ("
219 << getPosition("next-server", class_def_cfg) << ")");
220 }
221
222 if (next_server.getFamily() != AF_INET) {
223 isc_throw(DhcpConfigError, "Invalid next-server value: '"
224 << next_server_txt << "', must be IPv4 address ("
225 << getPosition("next-server", class_def_cfg) << ")");
226 }
227
228 if (next_server.isV4Bcast()) {
229 isc_throw(DhcpConfigError, "Invalid next-server value: '"
230 << next_server_txt << "', must not be a broadcast ("
231 << getPosition("next-server", class_def_cfg) << ")");
232 }
233 }
234
235 // Let's try to parse server-hostname
236 std::string sname;
237 if (class_def_cfg->contains("server-hostname")) {
238 sname = getString(class_def_cfg, "server-hostname");
239
240 if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
241 isc_throw(DhcpConfigError, "server-hostname must be at most "
242 << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
243 << sname.length() << " ("
244 << getPosition("server-hostname", class_def_cfg) << ")");
245 }
246 }
247
248 // Let's try to parse boot-file-name
249 std::string filename;
250 if (class_def_cfg->contains("boot-file-name")) {
251 filename = getString(class_def_cfg, "boot-file-name");
252
253 if (filename.length() > Pkt4::MAX_FILE_LEN) {
254 isc_throw(DhcpConfigError, "boot-file-name must be at most "
255 << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
256 << filename.length() << " ("
257 << getPosition("boot-file-name", class_def_cfg) << ")");
258 }
259 }
260
261 Optional<uint32_t> offer_lft;
262 if (class_def_cfg->contains("offer-lifetime")) {
263 auto value = getInteger(class_def_cfg, "offer-lifetime");
264 if (value < 0) {
265 isc_throw(DhcpConfigError, "the value of offer-lifetime '"
266 << value << "' must be a positive number ("
267 << getPosition("offer-lifetime", class_def_cfg) << ")");
268 }
269
270 offer_lft = value;
271 }
272
273 // Parse valid lifetime triplet.
274 Triplet<uint32_t> valid_lft = parseIntTriplet(class_def_cfg, "valid-lifetime");
275
276 Triplet<uint32_t> preferred_lft;
277 if (family != AF_INET) {
278 // Parse preferred lifetime triplet.
279 preferred_lft = parseIntTriplet(class_def_cfg, "preferred-lifetime");
280 }
281
282 // Sanity checks on built-in classes
283 for (auto const& bn : builtinNames) {
284 if (name == bn) {
285 if (additional) {
286 isc_throw(DhcpConfigError, "built-in class '" << name
287 << "' only-in-additional-list flag must be false");
288 }
289 if (!test.empty()) {
290 isc_throw(DhcpConfigError, "built-in class '" << name
291 << "' test expression must be empty");
292 }
293 }
294 }
295
296 // Sanity checks on DROP and REJECT
297 if ((name == "DROP") || (name == "REJECT")) {
298 if (additional) {
299 isc_throw(DhcpConfigError, "special class '" << name
300 << "' only-in-additional-list flag must be false");
301 }
302 // depend_on_known is now allowed
303 }
304
305 if (additional &&
306 (!valid_lft.unspecified() ||
307 !preferred_lft.unspecified() ||
308 !offer_lft.unspecified())) {
310 .arg(name);
311 }
312
313 // Add the client class definition
314 try {
315 class_dictionary->addClass(name, match_expr, test, additional,
316 depend_on_known, options, defs,
317 user_context, next_server, sname, filename,
318 valid_lft, preferred_lft, is_template, offer_lft);
319 } catch (const std::exception& ex) {
320 std::ostringstream s;
321 s << "Can't add class: " << ex.what();
322 // Append position of the error in JSON string if required.
323 if (append_error_position) {
324 s << " (" << class_def_cfg->getPosition() << ")";
325 }
326 isc_throw(DhcpConfigError, s.str());
327 }
328}
329
330void
332 const uint16_t family) {
333 // Make sure that the client class definition is stored in a map.
334 if (!class_def_cfg || (class_def_cfg->getType() != Element::map)) {
335 isc_throw(DhcpConfigError, "client class definition is not a map");
336 }
337
338 // Common v4 and v6 parameters supported for the client class.
339 static std::set<std::string> supported_params = { "name",
340 "test",
341 "option-data",
342 "user-context",
343 "only-in-additional-list",
344 "valid-lifetime",
345 "min-valid-lifetime",
346 "max-valid-lifetime",
347 "template-test"};
348
349 // The v4 client class supports additional parameters.
350 static std::set<std::string> supported_params_v4 = { "option-def",
351 "next-server",
352 "server-hostname",
353 "boot-file-name" };
354
355 // The v6 client class supports additional parameters.
356 static std::set<std::string> supported_params_v6 = { "preferred-lifetime",
357 "min-preferred-lifetime",
358 "max-preferred-lifetime" };
359
360 // Iterate over the specified parameters and check if they are all supported.
361 for (auto const& name_value_pair : class_def_cfg->mapValue()) {
362 if ((supported_params.count(name_value_pair.first) > 0) ||
363 ((family == AF_INET) && (supported_params_v4.count(name_value_pair.first) > 0)) ||
364 ((family != AF_INET) && (supported_params_v6.count(name_value_pair.first) > 0))) {
365 continue;
366 } else {
367 isc_throw(DhcpConfigError, "unsupported client class parameter '"
368 << name_value_pair.first << "'");
369 }
370 }
371}
372
373boost::shared_ptr<OptionDataListParser>
375 CfgOptionDefPtr cfg_option_def) const {
376 auto parser = boost::make_shared<OptionDataListParser>(address_family, cfg_option_def);
377 return (parser);
378}
379
380// ****************** ClientClassDefListParser ************************
381
384 uint16_t family, bool check_dependencies) {
386 for (auto const& client_class_def : client_class_def_list->listValue()) {
388 parser.parse(dictionary, client_class_def, family, true, check_dependencies);
389 }
390 return (dictionary);
391}
392
393} // end of namespace isc::dhcp
394} // end of namespace isc
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 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:405
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.
static std::string escape(const std::string &name, bool escape_escape=true)
Escape a client class name.
Definition classify.cc:164
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:286
Parser for a single option definition.
OptionDefinitionPtr parse(isc::data::ConstElementPtr option_def)
Parses an entry that describes single option definition.
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.
std::function< bool(const ClientClass &)> CheckDefined
Type of the check defined function.
bool parseString(const std::string &str, ParserType type=PARSER_BOOL)
Run the parser on the string specified.
ParserType
Specifies what type of expression the parser is expected to see.
@ PARSER_BOOL
expression is expected to evaluate to bool
@ PARSER_STRING
expression is expected to evaluate to string
static bool acceptAll(const ClientClass &client_class)
Accept all client class names.
isc::dhcp::Expression expression_
Parsed expression (output tokens are stored here).
A template representing an optional value.
Definition optional.h:37
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition optional.h:145
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.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition dhcpsrv_log.h:56
std::string ClientClass
Defines a single class name.
Definition classify.h:45
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition cfg_option.h:983
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
const isc::log::MessageID DHCPSRV_CLASS_WITH_ADDITIONAL_AND_LIFETIMES
const isc::log::MessageID DHCPSRV_CLASS_BAD_NAME
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
boost::shared_ptr< Expression > ExpressionPtr
Definition token.h:33
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:31
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.