1 // Copyright (C) 2016-2020 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 <dhcpsrv/cfg_db_access.h>
9 #include <dhcpsrv/db_type.h>
10 #include <dhcpsrv/host_data_source_factory.h>
11 #include <dhcpsrv/host_mgr.h>
12 #include <dhcpsrv/lease_mgr_factory.h>
13 #include <boost/algorithm/string.hpp>
14 #include <boost/foreach.hpp>
15 #include <boost/lexical_cast.hpp>
16 #include <sstream>
17 #include <vector>
18 
19 using namespace isc::data;
20 
21 namespace isc {
22 namespace dhcp {
23 
CfgDbAccess()24 CfgDbAccess::CfgDbAccess()
25     : appended_parameters_(), lease_db_access_("type=memfile"),
26       host_db_access_(), ip_reservations_unique_(true) {
27 }
28 
29 std::string
getLeaseDbAccessString() const30 CfgDbAccess::getLeaseDbAccessString() const {
31     return (getAccessString(lease_db_access_));
32 }
33 
34 
35 std::string
getHostDbAccessString() const36 CfgDbAccess::getHostDbAccessString() const {
37     if (host_db_access_.empty()) {
38         return ("");
39     } else {
40         return (getAccessString(host_db_access_.front()));
41     }
42 }
43 
44 std::list<std::string>
getHostDbAccessStringList() const45 CfgDbAccess::getHostDbAccessStringList() const {
46     std::list<std::string> ret;
47     for (const std::string& dbaccess : host_db_access_) {
48         if (!dbaccess.empty()) {
49             ret.push_back(getAccessString(dbaccess));
50         }
51     }
52     return (ret);
53 }
54 
55 void
createManagers() const56 CfgDbAccess::createManagers() const {
57     // Recreate lease manager.
58     LeaseMgrFactory::destroy();
59     LeaseMgrFactory::create(getLeaseDbAccessString());
60 
61     // Recreate host data source.
62     HostMgr::create();
63 
64     // Restore the host cache.
65     if (HostDataSourceFactory::registeredFactory("cache")) {
66         HostMgr::addBackend("type=cache");
67     }
68 
69     // Add database backends.
70     std::list<std::string> host_db_access_list = getHostDbAccessStringList();
71     for (std::string& hds : host_db_access_list) {
72         HostMgr::addBackend(hds);
73     }
74 
75     // Check for a host cache.
76     HostMgr::checkCacheBackend(true);
77 
78     // Populate the ip-reservations-unique global setting to HostMgr.
79     // This operation may fail if any of the host backends does not support
80     // the new setting. We throw an exception here to signal configuration
81     // error. The exception does not contain the backend name but the called
82     // function in HostMgr logs a warning message that contains the name of
83     // the backend.
84     if (!HostMgr::instance().setIPReservationsUnique(ip_reservations_unique_)) {
85         isc_throw(InvalidOperation, "unable to configure the server to allow "
86                   "non unique IP reservations (ip-reservations-unique=false) "
87                   "because some host backends in use do not support this "
88                   "setting");
89     }
90 }
91 
92 std::string
getAccessString(const std::string & access_string) const93 CfgDbAccess::getAccessString(const std::string& access_string) const {
94     std::ostringstream s;
95     s << access_string;
96     // Only append additional parameters if any parameters are specified
97     // in a configuration. For host database, no parameters mean that
98     // database access is disabled and thus we don't want to append any
99     // parameters.
100     if ((s.tellp() != std::streampos(0)) && (!appended_parameters_.empty())) {
101         s << " " << appended_parameters_;
102     }
103 
104     return (s.str());
105 }
106 
107 } // end of isc::dhcp namespace
108 } // end of isc namespace
109