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
// 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 <asiolink/io_address.h>
#include <cc/data.h>
#include <dhcpsrv/cfgmgr.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/parsers/option_data_parser.h>
#include <dhcpsrv/parsers/shared_network_parser.h>
#include <dhcpsrv/parsers/simple_parser4.h>
#include <dhcpsrv/parsers/simple_parser6.h>
#include <dhcpsrv/shared_network.h>
#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

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

namespace isc {
namespace dhcp {

SharedNetwork4Parser::SharedNetwork4Parser(bool check_iface)
    : check_iface_(check_iface) {
}

SharedNetwork4Ptr
SharedNetwork4Parser::parse(const data::ConstElementPtr& shared_network_data,
                            bool encapsulate_options) {
    SharedNetwork4Ptr shared_network;
    try {

        // Check parameters.
        checkKeywords(SimpleParser4::SHARED_NETWORK4_PARAMETERS,
                      shared_network_data);

        // Make sure that the network name has been specified. The name is required
        // to create a SharedNetwork4 object.
        std::string name = getString(shared_network_data, "name");
        shared_network.reset(new SharedNetwork4(name));

        // Move from reservation mode to new reservations flags.
        ElementPtr mutable_params;
        mutable_params = boost::const_pointer_cast<Element>(shared_network_data);
        BaseNetworkParser::moveReservationMode(mutable_params);

        // Parse parameters common to all Network derivations.
        NetworkPtr network = boost::dynamic_pointer_cast<Network>(shared_network);
        parseCommon(mutable_params, network);

        // interface is an optional parameter
        if (shared_network_data->contains("interface")) {
            std::string iface = getString(shared_network_data, "interface");
            if (!iface.empty()) {
                if (check_iface_ && !IfaceMgr::instance().getIface(iface)) {
                    ConstElementPtr error =
                        shared_network_data->get("interface");
                    isc_throw(DhcpConfigError,
                              "Specified network interface name " << iface
                              << " for shared network " << name
                              << " is not present in the system ("
                              << error->getPosition() << ")");
                }
                shared_network->setIface(iface);
            }
        }

        if (shared_network_data->contains("option-data")) {
            auto json = shared_network_data->get("option-data");
            // Create parser instance for option-data.
            CfgOptionPtr cfg_option = shared_network->getCfgOption();
            auto parser = createOptionDataListParser();
            parser->parse(cfg_option, json, encapsulate_options);
        }

        if (shared_network_data->contains("subnet4")) {
            auto json = shared_network_data->get("subnet4");

            // Create parser instance of subnet4.
            auto parser = createSubnetsListParser();
            Subnet4Collection subnets;
            parser->parse(subnets, json);

            // Add all returned subnets into shared network.
            for (auto const& subnet : subnets) {
                shared_network->add(subnet);
            }
        }

        if (shared_network_data->contains("match-client-id")) {
            shared_network->setMatchClientId(getBoolean(shared_network_data,
                                                        "match-client-id"));
        }

        if (shared_network_data->contains("authoritative")) {
            shared_network->setAuthoritative(getBoolean(shared_network_data,
                                                        "authoritative"));
        }

        // Set next-server
        if (shared_network_data->contains("next-server")) {
            std::string next_server;
            try {
                next_server = getString(shared_network_data, "next-server");
                if (!next_server.empty()) {
                    shared_network->setSiaddr(IOAddress(next_server));
                }
            } catch (...) {
                ConstElementPtr next = shared_network_data->get("next-server");
                std::string pos;
                if (next) {
                    pos = next->getPosition().str();
                } else {
                    pos = shared_network_data->getPosition().str();
                }
                isc_throw(DhcpConfigError, "invalid parameter next-server : "
                          << next_server << "(" << pos << ")");
            }
        }

        // Set server-hostname.
        if (shared_network_data->contains("server-hostname")) {
            std::string sname = getString(shared_network_data, "server-hostname");
            if (!sname.empty()) {
                if (sname.length() >= Pkt4::MAX_SNAME_LEN) {
                    ConstElementPtr error = shared_network_data->get("server-hostname");
                    isc_throw(DhcpConfigError, "server-hostname must be at most "
                              << Pkt4::MAX_SNAME_LEN - 1 << " bytes long, it is "
                              << sname.length() << " ("
                              << error->getPosition() << ")");
                }
                shared_network->setSname(sname);
            }
        }

        // Set boot-file-name.
        if (shared_network_data->contains("boot-file-name")) {
            std::string filename = getString(shared_network_data, "boot-file-name");
            if (!filename.empty()) {
                if (filename.length() > Pkt4::MAX_FILE_LEN) {
                    ConstElementPtr error = shared_network_data->get("boot-file-name");
                    isc_throw(DhcpConfigError, "boot-file-name must be at most "
                              << Pkt4::MAX_FILE_LEN - 1 << " bytes long, it is "
                              << filename.length() << " ("
                              << error->getPosition() << ")");
                }
                shared_network->setFilename(filename);
            }
        }

        if (shared_network_data->contains("client-class")) {
            std::string client_class = getString(shared_network_data, "client-class");
            if (!client_class.empty()) {
                shared_network->allowClientClass(client_class);
            }
        }

        ConstElementPtr user_context = shared_network_data->get("user-context");
        if (user_context) {
            shared_network->setContext(user_context);
        }

        if (shared_network_data->contains("require-client-classes")) {
            const std::vector<data::ElementPtr>& class_list =
                shared_network_data->get("require-client-classes")->listValue();
            for (auto const& cclass : class_list) {
                if ((cclass->getType() != Element::string) ||
                    cclass->stringValue().empty()) {
                    isc_throw(DhcpConfigError, "invalid class name ("
                              << cclass->getPosition() << ")");
                }
                shared_network->requireClientClass(cclass->stringValue());
            }
        }

        if (shared_network_data->contains("relay")) {
            auto relay_parms = shared_network_data->get("relay");
            if (relay_parms) {
                RelayInfoParser parser(Option::V4);
                Network::RelayInfoPtr relay_info(new Network::RelayInfo());
                parser.parse(relay_info, relay_parms);
                shared_network->setRelayInfo(*relay_info);
            }
        }

        parseTeePercents(shared_network_data, network);

        // Parse DDNS parameters
        parseDdnsParams(shared_network_data, network);

        // Parse lease cache parameters
        parseCacheParams(shared_network_data, network);

        // Parse allocator params.
        parseAllocatorParams(shared_network_data, network);

        // Parse offer-lifetime parameter.
        Network4Ptr network4 = boost::dynamic_pointer_cast<Network4>(shared_network);
        parseOfferLft(shared_network_data, network4);

    } catch (const DhcpConfigError&) {
        // Position was already added
        throw;
    } catch (const std::exception& ex) {
        isc_throw(DhcpConfigError, ex.what() << " ("
                  << shared_network_data->getPosition() << ")");
    }

    // In order to take advantage of the dynamic inheritance of global
    // parameters to a shared network we need to set a callback function
    // for each shared network to allow for fetching global parameters.
     shared_network->setFetchGlobalsFn([]() -> ConstCfgGlobalsPtr {
        return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
    });

    return (shared_network);
}

boost::shared_ptr<OptionDataListParser>
SharedNetwork4Parser::createOptionDataListParser() const {
    auto parser = boost::make_shared<OptionDataListParser>(AF_INET);
    return (parser);
}

boost::shared_ptr<Subnets4ListConfigParser>
SharedNetwork4Parser::createSubnetsListParser() const {
    auto parser = boost::make_shared<Subnets4ListConfigParser>(check_iface_);
    return (parser);
}

SharedNetwork6Parser::SharedNetwork6Parser(bool check_iface)
    : check_iface_(check_iface) {
}

SharedNetwork6Ptr
SharedNetwork6Parser::parse(const data::ConstElementPtr& shared_network_data,
                            bool encapsulate_options) {
    SharedNetwork6Ptr shared_network;
    std::string name;<--- Shadowed declaration
    try {
        // Check parameters.
        checkKeywords(SimpleParser6::SHARED_NETWORK6_PARAMETERS,
                      shared_network_data);

        // Make sure that the network name has been specified. The name is required
        // to create a SharedNetwork6 object.
        std::string name = getString(shared_network_data, "name");<--- Shadow variable
        shared_network.reset(new SharedNetwork6(name));

        // Move from reservation mode to new reservations flags.
        ElementPtr mutable_params;
        mutable_params = boost::const_pointer_cast<Element>(shared_network_data);
        BaseNetworkParser::moveReservationMode(mutable_params);

        // Parse parameters common to all Network derivations.
        NetworkPtr network = boost::dynamic_pointer_cast<Network>(shared_network);
        parseCommon(mutable_params, network);

        // preferred-lifetime
        shared_network->setPreferred(parseIntTriplet(shared_network_data,
                                                     "preferred-lifetime"));

        // Get interface-id option content. For now we support string
        // representation only
        Optional<std::string> ifaceid;
        if (shared_network_data->contains("interface-id")) {
            ifaceid = getString(shared_network_data, "interface-id");
        }

        // Interface is an optional parameter
        Optional<std::string> iface;
        if (shared_network_data->contains("interface")) {
            iface = getString(shared_network_data, "interface");
        }

        // Specifying both interface for locally reachable subnets and
        // interface id for relays is mutually exclusive. Need to test for
        // this condition.
        if (!ifaceid.unspecified() && !iface.unspecified() && !ifaceid.empty() &&
            !iface.empty()) {
            isc_throw(isc::dhcp::DhcpConfigError,
                      "parser error: interface (defined for locally reachable "
                      "subnets) and interface-id (defined for subnets reachable"
                      " via relays) cannot be defined at the same time for "
                      "shared network " << name << "("
                      << shared_network_data->getPosition() << ")");
        }

        // Configure interface-id for remote interfaces, if defined
        if (!ifaceid.unspecified() && !ifaceid.empty()) {
            std::string ifaceid_value = ifaceid.get();
            OptionBuffer tmp(ifaceid_value.begin(), ifaceid_value.end());
            OptionPtr opt(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
            shared_network->setInterfaceId(opt);
        }

        // Set interface name. If it is defined, then subnets are available
        // directly over specified network interface.
        if (!iface.unspecified() && !iface.empty()) {
            if (check_iface_ && !IfaceMgr::instance().getIface(iface)) {
                ConstElementPtr error = shared_network_data->get("interface");
                isc_throw(DhcpConfigError,
                          "Specified network interface name " << iface
                          << " for shared network " << name
                          << " is not present in the system ("
                          << error->getPosition() << ")");
            }
            shared_network->setIface(iface);
        }

        if (shared_network_data->contains("rapid-commit")) {
            shared_network->setRapidCommit(getBoolean(shared_network_data,
                                                      "rapid-commit"));
        }

        if (shared_network_data->contains("option-data")) {
            auto json = shared_network_data->get("option-data");
            // Create parser instance for option-data.
            CfgOptionPtr cfg_option = shared_network->getCfgOption();
            auto parser = createOptionDataListParser();
            parser->parse(cfg_option, json, encapsulate_options);
        }

        if (shared_network_data->contains("client-class")) {
            std::string client_class = getString(shared_network_data, "client-class");
            if (!client_class.empty()) {
                shared_network->allowClientClass(client_class);
            }
        }

        ConstElementPtr user_context = shared_network_data->get("user-context");
        if (user_context) {
            shared_network->setContext(user_context);
        }

        if (shared_network_data->contains("require-client-classes")) {
            const std::vector<data::ElementPtr>& class_list =
                shared_network_data->get("require-client-classes")->listValue();
            for (auto const& cclass : class_list) {
                if ((cclass->getType() != Element::string) ||
                    cclass->stringValue().empty()) {
                    isc_throw(DhcpConfigError, "invalid class name ("
                              << cclass->getPosition() << ")");
                }
                shared_network->requireClientClass(cclass->stringValue());
            }
        }

        if (shared_network_data->contains("subnet6")) {
            auto json = shared_network_data->get("subnet6");

            // Create parser instance of subnet6.
            auto parser = createSubnetsListParser();
            Subnet6Collection subnets;
            parser->parse(subnets, json);

            // Add all returned subnets into shared network.
            for (auto const& subnet : subnets) {
                shared_network->add(subnet);
            }
        }

        if (shared_network_data->contains("relay")) {
            auto relay_parms = shared_network_data->get("relay");
            if (relay_parms) {
                RelayInfoParser parser(Option::V6);
                Network::RelayInfoPtr relay_info(new Network::RelayInfo());
                parser.parse(relay_info, relay_parms);
                shared_network->setRelayInfo(*relay_info);
            }
        }

        parseTeePercents(shared_network_data, network);

        // Parse DDNS parameters
        parseDdnsParams(shared_network_data, network);

        // Parse lease cache parameters
        parseCacheParams(shared_network_data, network);

        // Parse allocator params.
        parseAllocatorParams(shared_network_data, network);
        if (network->getAllocatorType() == "flq") {
            isc_throw(BadValue, "Free Lease Queue allocator is not supported for IPv6 address pools");
        }

        // Parse prefix delegation allocator params.
        auto network6 = boost::dynamic_pointer_cast<Network6>(shared_network);
        parsePdAllocatorParams(shared_network_data, network6);

    } catch (const std::exception& ex) {
        isc_throw(DhcpConfigError, ex.what() << " ("
                  << shared_network_data->getPosition() << ")");
    }

    // In order to take advantage of the dynamic inheritance of global
    // parameters to a shared network we need to set a callback function
    // for each shared network which can be used to fetch global parameters.
     shared_network->setFetchGlobalsFn([]() -> ConstCfgGlobalsPtr {
        return (CfgMgr::instance().getCurrentCfg()->getConfiguredGlobals());
    });

    return (shared_network);
}

boost::shared_ptr<OptionDataListParser>
SharedNetwork6Parser::createOptionDataListParser() const {
    auto parser = boost::make_shared<OptionDataListParser>(AF_INET6);
    return (parser);
}

boost::shared_ptr<Subnets6ListConfigParser>
SharedNetwork6Parser::createSubnetsListParser() const {
    auto parser = boost::make_shared<Subnets6ListConfigParser>(check_iface_);
    return (parser);
}

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