Kea 1.9.11
client_class_def_parser.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2021 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>
16#include <eval/eval_context.h>
17#include <asiolink/io_address.h>
18#include <asiolink/io_error.h>
19
20#include <boost/foreach.hpp>
21#include <algorithm>
22#include <sstream>
23
24using namespace isc::data;
25using namespace isc::asiolink;
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 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 try {
53 EvalContext eval_ctx(family == AF_INET ? Option::V4 : Option::V6,
54 check_defined);
55 eval_ctx.parseString(value);
56 expression.reset(new Expression());
57 *expression = eval_ctx.expression;
58 } catch (const std::exception& ex) {
59 // Append position if there is a failure.
61 "expression: [" << value
62 << "] error: " << ex.what() << " at ("
63 << expression_cfg->getPosition() << ")");
64 }
65}
66
67// ********************** ClientClassDefParser ****************************
68
69void
71 ConstElementPtr class_def_cfg,
72 uint16_t family,
73 bool append_error_position,
74 bool check_dependencies) {
75 // name is now mandatory, so let's deal with it first.
76 std::string name = getString(class_def_cfg, "name");
77 if (name.empty()) {
79 "not empty parameter 'name' is required "
80 << getPosition("name", class_def_cfg) << ")");
81 }
82
83 // Parse matching expression
84 ExpressionPtr match_expr;
85 ConstElementPtr test_cfg = class_def_cfg->get("test");
86 std::string test;
87 bool depend_on_known = false;
88 if (test_cfg) {
89 ExpressionParser parser;
90 auto check_defined =
91 [&class_dictionary, &depend_on_known, check_dependencies]
92 (const ClientClass& cclass) {
93 return (!check_dependencies || isClientClassDefined(class_dictionary,
94 depend_on_known,
95 cclass));
96 };
97 parser.parse(match_expr, test_cfg, family, check_defined);
98 test = test_cfg->stringValue();
99 }
100
101 // Parse option def
102 CfgOptionDefPtr defs(new CfgOptionDef());
103 ConstElementPtr option_defs = class_def_cfg->get("option-def");
104 if (option_defs) {
105 // Apply defaults
106 SimpleParser::setListDefaults(option_defs,
107 family == AF_INET ?
110
111 OptionDefParser parser(family);
112 BOOST_FOREACH(ConstElementPtr option_def, option_defs->listValue()) {
113 OptionDefinitionPtr def = parser.parse(option_def);
114
115 // Verify if the definition is for an option which is in a deferred
116 // processing list.
117 if (!LibDHCP::shouldDeferOptionUnpack(def->getOptionSpaceName(),
118 def->getCode())) {
120 "Not allowed option definition for code '"
121 << def->getCode() << "' in space '"
122 << def->getOptionSpaceName() << "' at ("
123 << option_def->getPosition() << ")");
124 }
125 try {
126 defs->add(def);
127 } catch (const std::exception& ex) {
128 // Sanity check: it should never happen
129 isc_throw(DhcpConfigError, ex.what() << " ("
130 << option_def->getPosition() << ")");
131 }
132 }
133 }
134
135 // Parse option data
136 CfgOptionPtr options(new CfgOption());
137 ConstElementPtr option_data = class_def_cfg->get("option-data");
138 if (option_data) {
139 OptionDataListParser opts_parser(family, defs);
140 opts_parser.parse(options, option_data);
141 }
142
143 // Parse user context
144 ConstElementPtr user_context = class_def_cfg->get("user-context");
145
146 // Let's try to parse the only-if-required flag
147 bool required = false;
148 if (class_def_cfg->contains("only-if-required")) {
149 required = getBoolean(class_def_cfg, "only-if-required");
150 }
151
152 // Let's try to parse the next-server field
153 IOAddress next_server("0.0.0.0");
154 if (class_def_cfg->contains("next-server")) {
155 std::string next_server_txt = getString(class_def_cfg, "next-server");
156 try {
157 next_server = IOAddress(next_server_txt);
158 } catch (const IOError& ex) {
160 "Invalid next-server value specified: '"
161 << next_server_txt << "' ("
162 << getPosition("next-server", class_def_cfg) << ")");
163 }
164
165 if (next_server.getFamily() != AF_INET) {
166 isc_throw(DhcpConfigError, "Invalid next-server value: '"
167 << next_server_txt << "', must be IPv4 address ("
168 << getPosition("next-server", class_def_cfg) << ")");
169 }
170
171 if (next_server.isV4Bcast()) {
172 isc_throw(DhcpConfigError, "Invalid next-server value: '"
173 << next_server_txt << "', must not be a broadcast ("
174 << getPosition("next-server", class_def_cfg) << ")");
175 }
176 }
177
178 // Let's try to parse server-hostname
179 std::string sname;
180 if (class_def_cfg->contains("server-hostname")) {
181 sname = getString(class_def_cfg, "server-hostname");
182
183 if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
184 isc_throw(DhcpConfigError, "server-hostname must be at most "
185 << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
186 << sname.length() << " ("
187 << getPosition("server-hostname", class_def_cfg) << ")");
188 }
189 }
190
191 // Let's try to parse boot-file-name
192 std::string filename;
193 if (class_def_cfg->contains("boot-file-name")) {
194 filename = getString(class_def_cfg, "boot-file-name");
195
196 if (filename.length() > Pkt4::MAX_FILE_LEN) {
197 isc_throw(DhcpConfigError, "boot-file-name must be at most "
198 << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
199 << filename.length() << " ("
200 << getPosition("boot-file-name", class_def_cfg) << ")");
201 }
202
203 }
204
205 // Parse valid lifetime triplet.
206 Triplet<uint32_t> valid_lft = parseIntTriplet(class_def_cfg, "valid-lifetime");
207
208 Triplet<uint32_t> preferred_lft;
209 if (family != AF_INET) {
210 // Parse preferred lifetime triplet.
211 preferred_lft = parseIntTriplet(class_def_cfg, "preferred-lifetime");
212 }
213
214 // Sanity checks on built-in classes
215 for (auto bn : builtinNames) {
216 if (name == bn) {
217 if (required) {
218 isc_throw(DhcpConfigError, "built-in class '" << name
219 << "' only-if-required flag must be false");
220 }
221 if (!test.empty()) {
222 isc_throw(DhcpConfigError, "built-in class '" << name
223 << "' test expression must be empty");
224 }
225 }
226 }
227
228 // Sanity checks on DROP
229 if (name == "DROP") {
230 if (required) {
231 isc_throw(DhcpConfigError, "special class '" << name
232 << "' only-if-required flag must be false");
233 }
234 // depend_on_known is now allowed
235 }
236
237 // Add the client class definition
238 try {
239 class_dictionary->addClass(name, match_expr, test, required,
240 depend_on_known, options, defs,
241 user_context, next_server, sname, filename,
242 valid_lft, preferred_lft);
243 } catch (const std::exception& ex) {
244 std::ostringstream s;
245 s << "Can't add class: " << ex.what();
246 // Append position of the error in JSON string if required.
247 if (append_error_position) {
248 s << " (" << class_def_cfg->getPosition() << ")";
249 }
250 isc_throw(DhcpConfigError, s.str());
251 }
252}
253
254void
256 const uint16_t family) {
257 // Make sure that the client class definition is stored in a map.
258 if (!class_def_cfg || (class_def_cfg->getType() != Element::map)) {
259 isc_throw(DhcpConfigError, "client class definition is not a map");
260 }
261
262 // Common v4 and v6 parameters supported for the client class.
263 static std::set<std::string> supported_params = { "name",
264 "test",
265 "option-data",
266 "user-context",
267 "only-if-required",
268 "valid-lifetime",
269 "min-valid-lifetime",
270 "max-valid-lifetime" };
271
272
273 // The v4 client class supports additional parameters.
274 static std::set<std::string> supported_params_v4 = { "option-def",
275 "next-server",
276 "server-hostname",
277 "boot-file-name" };
278
279 // The v6 client class supports additional parameters.
280 static std::set<std::string> supported_params_v6 = { "preferred-lifetime",
281 "min-preferred-lifetime",
282 "max-preferred-lifetime" };
283
284 // Iterate over the specified parameters and check if they are all supported.
285 for (auto name_value_pair : class_def_cfg->mapValue()) {
286 if ((supported_params.count(name_value_pair.first) > 0) ||
287 ((family == AF_INET) && (supported_params_v4.count(name_value_pair.first) > 0)) ||
288 ((family != AF_INET) && (supported_params_v6.count(name_value_pair.first) > 0))) {
289 continue;
290 } else {
291 isc_throw(DhcpConfigError, "unsupported client class parameter '"
292 << name_value_pair.first << "'");
293 }
294 }
295}
296
297
298// ****************** ClientClassDefListParser ************************
299
302 uint16_t family, bool check_dependencies) {
304 BOOST_FOREACH(ConstElementPtr client_class_def,
305 client_class_def_list->listValue()) {
307 parser.parse(dictionary, client_class_def, family, true, check_dependencies);
308 }
309 return (dictionary);
310}
311
312} // end of namespace isc::dhcp
313} // end of namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
const dhcp::Triplet< uint32_t > parseIntTriplet(const data::ConstElementPtr &scope, const std::string &name)
Parses an integer triplet.
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.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
Represents option definitions used by the DHCP server.
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:314
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.
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)
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:278
Parser for option data values within a subnet.
void parse(const CfgOptionPtr &cfg, isc::data::ConstElementPtr option_data_list)
Parses a list of options, instantiates them and stores in cfg.
Parser for a single option definition.
Definition: dhcp_parsers.h:227
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.
This template specifies a parameter value.
Definition: triplet.h:37
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
isc::dhcp::Expression expression
Parsed expression (output tokens are stored here)
Definition: eval_context.h:67
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:27
std::string ClientClass
Defines a single class name.
Definition: classify.h:37
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:706
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 built-in client class names.
Defines the logger used by the top-level component of kea-lfc.