1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (C) 2017-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <exceptions/exceptions.h>
#include <dhcp/dhcp4.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/option_definition.h>
#include <dhcp/option_space.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/parsers/option_data_parser.h>
#include <dhcpsrv/parsers/simple_parser4.h>
#include <dhcpsrv/parsers/simple_parser6.h>
#include <util/encode/encode.h>
#include <util/str.h>
#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::data;
using namespace isc::util;

namespace isc {
namespace dhcp {

// **************************** OptionDataParser *************************

OptionDataParser::OptionDataParser(const uint16_t address_family,
                                   CfgOptionDefPtr cfg_option_def)
    : address_family_(address_family), cfg_option_def_(cfg_option_def) {
}

std::pair<OptionDescriptor, std::string>
OptionDataParser::parse(isc::data::ConstElementPtr single_option) {

    // Check parameters.
    if (address_family_ == AF_INET) {
        checkKeywords(SimpleParser4::OPTION4_PARAMETERS, single_option);
    } else {
        checkKeywords(SimpleParser6::OPTION6_PARAMETERS, single_option);
    }

    // Try to create the option instance.
    std::pair<OptionDescriptor, std::string> opt = createOption(single_option);

    if (!opt.first.option_) {
        // Should never happen (@todo: update message)
        isc_throw(isc::InvalidOperation,
            "parser logic error: no option has been configured and"
            " thus there is nothing to commit. Has build() been called?");
    }

    return (opt);
}

Optional<uint32_t>
OptionDataParser::extractCode(ConstElementPtr parent) const {
    uint32_t code;
    try {
        code = getInteger(parent, "code");

    } catch (const std::exception&) {
        // The code parameter was not found. Return an unspecified
        // value.
        return (Optional<uint32_t>());
    }

    if (address_family_ == AF_INET &&
        code > std::numeric_limits<uint8_t>::max()) {
        isc_throw(DhcpConfigError, "invalid option code '" << code
                  << "', it must not be greater than '"
                  << static_cast<int>(std::numeric_limits<uint8_t>::max())
                  << "' (" << getPosition("code", parent)
                  << ")");

    } else if (address_family_ == AF_INET6 &&
               code > std::numeric_limits<uint16_t>::max()) {
        isc_throw(DhcpConfigError, "invalid option code '" << code
                  << "', it must not exceed '"
                  << std::numeric_limits<uint16_t>::max()
                  << "' (" << getPosition("code", parent)
                  << ")");

    }

    return (Optional<uint32_t>(code));
}

Optional<std::string>
OptionDataParser::extractName(ConstElementPtr parent) const {
    std::string name;
    try {
        name = getString(parent, "name");

    } catch (...) {
        return (Optional<std::string>());
    }

    if (name.find(" ") != std::string::npos) {
        isc_throw(DhcpConfigError, "invalid option name '" << name
                  << "', space character is not allowed ("
                  << getPosition("name", parent) << ")");
    }

    return (Optional<std::string>(name));
}

Optional<std::string>
OptionDataParser::extractData(ConstElementPtr parent) const {
    std::string data;
    try {
        data = getString(parent, "data");

    } catch (...) {
        // The "data" parameter was not found. Return an empty value.
        return (Optional<std::string>());
    }

    return (Optional<std::string>(data));
}

Optional<bool>
OptionDataParser::extractCSVFormat(ConstElementPtr parent) const {
    bool csv_format = true;
    try {
        csv_format = getBoolean(parent, "csv-format");

    } catch (...) {
        return (Optional<bool>());
    }

    return (Optional<bool>(csv_format));
}

std::string
OptionDataParser::extractSpace(ConstElementPtr parent) const {
    std::string space = address_family_ == AF_INET ?
        DHCP4_OPTION_SPACE : DHCP6_OPTION_SPACE;
    try {
        space = getString(parent, "space");

    } catch (...) {
        return (space);
    }

    try {
        if (!OptionSpace::validateName(space)) {
            isc_throw(DhcpConfigError, "invalid option space name '"
                      << space << "'");
        }

        if ((space == DHCP4_OPTION_SPACE) && (address_family_ == AF_INET6)) {
            isc_throw(DhcpConfigError, "'" << DHCP4_OPTION_SPACE
                      << "' option space name is reserved for DHCPv4 server");

        } else if ((space == DHCP6_OPTION_SPACE) &&
                   (address_family_ == AF_INET)) {
            isc_throw(DhcpConfigError, "'" << DHCP6_OPTION_SPACE
                      << "' option space name is reserved for DHCPv6 server");
        }

    } catch (const std::exception& ex) {
        // Append position of the option space parameter.
        isc_throw(DhcpConfigError, ex.what() << " ("
                  << getPosition("space", parent) << ")");
    }

    return (space);
}

Optional<bool>
OptionDataParser::extractPersistent(ConstElementPtr parent) const {
    bool persist = false;
    try {
        persist = getBoolean(parent, "always-send");

    } catch (...) {
        return (Optional<bool>());
    }

    return (Optional<bool>(persist));
}

Optional<bool>
OptionDataParser::extractCancelled(ConstElementPtr parent) const {
    bool cancel = false;
    try {
        cancel = getBoolean(parent, "never-send");

    } catch (...) {
        return (Optional<bool>());
    }

    return (Optional<bool>(cancel));
}

OptionDefinitionPtr
OptionDataParser::findOptionDefinition(const std::string& option_space,
                                       const Optional<uint32_t>& option_code,
                                       const Optional<std::string>& option_name) const {
    OptionDefinitionPtr def;
    if (cfg_option_def_) {
        // Check if the definition was given in the constructor
        if (option_code.unspecified()) {
            def = cfg_option_def_->get(option_space, option_name);
        } else {
            def = cfg_option_def_->get(option_space, option_code);
        }
    }

    if (!def) {
        // Check if this is a standard option.
        if (option_code.unspecified()) {
            def = LibDHCP::getOptionDef(option_space, option_name);
        } else {
            def = LibDHCP::getOptionDef(option_space, option_code);
        }
    }

    if (!def) {
        // Check if this is a vendor-option. If it is, get vendor-specific
        // definition.
        uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(option_space);
        if (vendor_id) {
            const Option::Universe u = address_family_ == AF_INET ?
                Option::V4 : Option::V6;
            if (option_code.unspecified()) {
                def = LibDHCP::getVendorOptionDef(u, vendor_id, option_name);
            } else {
                def = LibDHCP::getVendorOptionDef(u, vendor_id, option_code);
            }
        }
    }

    if (!def) {
        // Check if this is an option specified by a user. We used to
        // check that in the staging configuration, but when the configuration
        // changes are caused by a command the staging configuration doesn't
        // exist. What is always available is the container holding runtime
        // option definitions in LibDHCP. It holds option definitions from
        // the staging configuration in case of the full reconfiguration or
        // the definitions from the current configuration in case there is
        // no staging configuration (after configuration commit). In other
        // words, runtime options are always the ones that we need here.
        if (option_code.unspecified()) {
            def = LibDHCP::getRuntimeOptionDef(option_space, option_name);
        } else {
            def = LibDHCP::getRuntimeOptionDef(option_space, option_code);
        }
    }

    if (!def) {
        // Finish by last resort definitions.
        if (option_code.unspecified()) {
            def = LibDHCP::getLastResortOptionDef(option_space, option_name);
        } else {
            def = LibDHCP::getLastResortOptionDef(option_space, option_code);
        }
    }

    return (def);
}

std::pair<OptionDescriptor, std::string>
OptionDataParser::createOption(ConstElementPtr option_data) {
    const Option::Universe universe = address_family_ == AF_INET ?
        Option::V4 : Option::V6;

    Optional<uint32_t> code_param = extractCode(option_data);
    Optional<std::string> name_param = extractName(option_data);
    Optional<bool> csv_format_param = extractCSVFormat(option_data);
    Optional<bool> persist_param = extractPersistent(option_data);
    Optional<bool> cancel_param = extractCancelled(option_data);
    Optional<std::string> data_param = extractData(option_data);
    std::string space_param = extractSpace(option_data);
    ConstElementPtr user_context = option_data->get("user-context");
    ConstElementPtr client_classes = option_data->get("client-classes");

    // Require that option code or option name is specified.
    if (code_param.unspecified() && name_param.unspecified()) {
        isc_throw(DhcpConfigError, "option data configuration requires one of"
                  " 'code' or 'name' parameters to be specified"
                  << " (" << option_data->getPosition() << ")");
    }

    // Try to find a corresponding option definition using option code or
    // option name.
    OptionDefinitionPtr def = findOptionDefinition(space_param, code_param, name_param);

    // If there is no definition, the user must not explicitly enable the
    // use of csv-format.
    if (!def) {
        // If explicitly requested that the CSV format is to be used,
        // the option definition is a must.
        if (!csv_format_param.unspecified() && csv_format_param) {
            isc_throw(DhcpConfigError, "definition for the option '"
                      << space_param << "." << name_param
                      << "' having code '" << code_param
                      << "' does not exist ("
                      << getPosition("name", option_data)
                      << ")");

        // If there is no option definition and the option code is not specified
        // we have no means to find the option code.
        } else if (!name_param.unspecified() && code_param.unspecified()) {
            isc_throw(DhcpConfigError, "definition for the option '"
                      << space_param << "." << name_param
                      << "' does not exist ("
                      << getPosition("name", option_data)
                      << ")");
        }
    } else {
        // Option name is specified it should match the name in the definition.
        if (!name_param.unspecified() && (def->getName() != name_param.get())) {
            isc_throw(DhcpConfigError, "specified option name '"
                      << name_param << "' does not match the "
                      << "option definition: '" << space_param
                      << "." << def->getName() << "' ("
                      << getPosition("name", option_data)
                      << ")");
        }
    }

    // No data and cancelled is a supported special case.
    if (!cancel_param.unspecified() && cancel_param &&
        data_param.unspecified()) {
        uint16_t code;
        if (def) {
            code = def->getCode();
        } else {
            code = static_cast<uint16_t>(code_param);
        }
        OptionPtr option(new Option(universe, code));
        bool persistent = !persist_param.unspecified() && persist_param;
        OptionDescriptor desc(option, persistent, true, "", user_context);
        return (make_pair(desc, space_param));
    }

    // Transform string of hexadecimal digits into binary format.
    std::vector<uint8_t> binary;
    std::vector<std::string> data_tokens;

    // If the definition is available and csv-format hasn't been explicitly
    // disabled, we will parse the data as comma separated values.
    if (def && (csv_format_param.unspecified() || csv_format_param)) {
        // If the option data is specified as a string of comma
        // separated values then we need to split this string into
        // individual values - each value will be used to initialize
        // one data field of an option.
        // It is the only usage of the escape option: this allows
        // to embed commas in individual values and to return
        // for instance a string value with embedded commas.
        data_tokens = isc::util::str::tokens(data_param, ",", true);

    } else {
        // Try to convert the values in quotes into a vector of ASCII codes.
        // If the identifier lacks opening and closing quote, this will return
        // an empty value, in which case we'll try to decode it as a string of
        // hexadecimal digits.
        try {
            binary = util::str::quotedStringToBinary(data_param);
            if (binary.empty()) {
                util::str::decodeFormattedHexString(data_param, binary);
            }
        } catch (...) {
            isc_throw(DhcpConfigError, "option data is not a valid"
                      << " string of hexadecimal digits: " << data_param
                      << " ("
                      << getPosition("data", option_data)
                      << ")");
        }
    }

    OptionDescriptor desc(false, false);

    if (!def) {
        // @todo We have a limited set of option definitions initialized at
        // the moment.  In the future we want to initialize option definitions
        // for all options.  Consequently an error will be issued if an option
        // definition does not exist for a particular option code. For now it is
        // ok to create generic option if definition does not exist.
        OptionPtr option(new Option(universe, static_cast<uint16_t>(code_param),
                                    binary));

        desc.option_ = option;
        desc.persistent_ = !persist_param.unspecified() && persist_param;
        desc.cancelled_ = !cancel_param.unspecified() && cancel_param;
    } else {
        // Option definition has been found so let's use it to create
        // an instance of our option.
        try {
            bool use_csv = csv_format_param.unspecified() || csv_format_param;
            OptionPtr option = use_csv ?
                def->optionFactory(universe, def->getCode(), data_tokens) :
                def->optionFactory(universe, def->getCode(), binary);
            desc.option_ = option;
            desc.persistent_ = !persist_param.unspecified() && persist_param;
            desc.cancelled_ = !cancel_param.unspecified() && cancel_param;
            if (use_csv) {
                desc.formatted_value_ = data_param;
            }
        } catch (const isc::Exception& ex) {
            isc_throw(DhcpConfigError, "option data does not match"
                      << " option definition (space: " << space_param
                      << ", code: " << def->getCode() << "): "
                      << ex.what() << " ("
                      << getPosition("data", option_data)
                      << ")");
        }
    }

    // Check PAD and END in (and only in) dhcp4 space.
    if (space_param == DHCP4_OPTION_SPACE) {
        if (desc.option_->getType() == DHO_PAD) {
            isc_throw(DhcpConfigError, "invalid option code '0': "
                      << "reserved for PAD ("
                      << option_data->getPosition() << ")");
        } else if (desc.option_->getType() == DHO_END) {
            isc_throw(DhcpConfigError, "invalid option code '255': "
                      << "reserved for END ("
                      << option_data->getPosition() << ")");
        }
    }

    // For dhcp6 space the value 0 is reserved.
    if (space_param == DHCP6_OPTION_SPACE) {
        if (desc.option_->getType() == 0) {
            isc_throw(DhcpConfigError, "invalid option code '0': "
                      << "reserved value ("
                      << option_data->getPosition() << ")");
        }
    }


    // Add user context
    if (user_context) {
        desc.setContext(user_context);
    }

#if 1
    desc.client_classes_.fromElement(client_classes);
#else
    if (client_classes) {
        for (auto const& class_element : client_classes->listValue()) {
            desc.addClientClass(class_element->stringValue());
        }
    }
#endif

    // All went good, so we can set the option space name.
    return (make_pair(desc, space_param));
}

// **************************** OptionDataListParser *************************
OptionDataListParser::OptionDataListParser(//const std::string&,
                                           //const CfgOptionPtr& cfg,
                                           const uint16_t address_family,
                                           CfgOptionDefPtr cfg_option_def)
    : address_family_(address_family), cfg_option_def_(cfg_option_def) {
}


void OptionDataListParser::parse(const CfgOptionPtr& cfg,
                                 isc::data::ConstElementPtr option_data_list,
                                 bool encapsulate) {
    auto option_parser = createOptionDataParser();
    for (auto const& data : option_data_list->listValue()) {
        std::pair<OptionDescriptor, std::string> option =
            option_parser->parse(data);
        // Use the option description to keep the formatted value
        cfg->add(option.first, option.second);
        if (encapsulate) {
            cfg->encapsulate();
        }
    }
}

boost::shared_ptr<OptionDataParser>
OptionDataListParser::createOptionDataParser() const {
    auto parser = boost::make_shared<OptionDataParser>(address_family_, cfg_option_def_);
    return (parser);
}

} // end of namespace isc::dhcp
} // end of namespace isc