summaryrefslogtreecommitdiffstats
path: root/src/lib/database/database_connection.cc
blob: 067a1e03d9ae2b3766db081b341939cef24e70f1 (plain)
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
// Copyright (C) 2015-2022 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 <cc/cfg_to_element.h>
#include <database/database_connection.h>
#include <database/db_exceptions.h>
#include <database/db_log.h>
#include <database/db_messages.h>
#include <exceptions/exceptions.h>
#include <util/strutil.h>

#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <vector>

using namespace isc::util;
using namespace std;

namespace isc {
namespace db {

const time_t DatabaseConnection::MAX_DB_TIME = 2147483647;

std::string
DatabaseConnection::getParameter(const std::string& name) const {
    ParameterMap::const_iterator param = parameters_.find(name);
    if (param == parameters_.end()) {
        isc_throw(BadValue, "Parameter " << name << " not found");
    }
    return (param->second);
}

DatabaseConnection::ParameterMap
DatabaseConnection::parse(const std::string& dbaccess) {
    DatabaseConnection::ParameterMap mapped_tokens;
    std::string dba = dbaccess;

    if (!dba.empty()) {
        try {
            vector<string> tokens;

            // Handle the special case of a password which is enclosed in apostrophes.
            // Such password may include whitespace.
            std::string password_prefix = "password='";
            auto password_pos = dba.find(password_prefix);
            if (password_pos != string::npos) {
                // Password starts with apostrophe, so let's find ending apostrophe.
                auto password_end_pos = dba.find('\'', password_pos + password_prefix.length());
                if (password_end_pos == string::npos) {
                    // No ending apostrophe. This is wrong.
                    isc_throw(InvalidParameter, "Apostrophe (') expected at the end of password");
                }
                // Extract the password value. It starts after the password=' prefix and ends
                // at the position of ending apostrophe.
                auto password = dba.substr(password_pos + password_prefix.length(),
                                           password_end_pos - password_pos - password_prefix.length());
                mapped_tokens.insert(make_pair("password", password));

                // We need to erase the password from the access string because the generic
                // algorithm parsing other parameters requires that there are no whitespaces
                // within the parameter values.
                dba.erase(password_pos, password_prefix.length() + password.length() + 2);
                // Leading or trailing whitespace may remain after the password removal.
                dba = util::str::trim(dba);
                // If the password was the only parameter in the access string, there is
                // nothing more to do.
                if (dba.empty()) {
                    return (mapped_tokens);
                }
            }

            // We need to pass a string to is_any_of, not just char*. Otherwise
            // there are cryptic warnings on Debian6 running g++ 4.4 in
            // /usr/include/c++/4.4/bits/stl_algo.h:2178 "array subscript is above
            // array bounds"
            boost::split(tokens, dba, boost::is_any_of(string("\t ")));
            BOOST_FOREACH(std::string token, tokens) {
                size_t pos = token.find("=");
                if (pos != string::npos) {
                    string name = token.substr(0, pos);
                    string value = token.substr(pos + 1);
                    mapped_tokens.insert(make_pair(name, value));
                } else {
                    isc_throw(InvalidParameter, "Cannot parse " << token
                              << ", expected format is name=value");
                }
            }
        } catch (const std::exception& ex) {
            // We'd obscure the password if we could parse the access string.
            DB_LOG_ERROR(DB_INVALID_ACCESS).arg(dbaccess);
            throw;
        }
    }

    return (mapped_tokens);
}

std::string
DatabaseConnection::redactedAccessString(const ParameterMap& parameters) {
    // Reconstruct the access string: start of with an empty string, then
    // work through all the parameters in the original string and add them.
    std::string access;
    for (DatabaseConnection::ParameterMap::const_iterator i = parameters.begin();
         i != parameters.end(); ++i) {

        // Separate second and subsequent tokens are preceded by a space.
        if (!access.empty()) {
            access += " ";
        }

        // Append name of parameter...
        access += i->first;
        access += "=";

        // ... and the value, except in the case of the password, where a
        // redacted value is appended.
        if (i->first == std::string("password")) {
            access += "*****";
        } else {
            access += i->second;
        }
    }

    return (access);
}

bool
DatabaseConnection::configuredReadOnly() const {
    std::string readonly_value = "false";
    try {
        readonly_value = getParameter("readonly");
        boost::algorithm::to_lower(readonly_value);
    } catch (...) {
        // Parameter "readonly" hasn't been specified so we simply use
        // the default value of "false".
    }

    if ((readonly_value != "false") && (readonly_value != "true")) {
        isc_throw(DbInvalidReadOnly, "invalid value '" << readonly_value
                  << "' specified for boolean parameter 'readonly'");
    }

    return (readonly_value == "true");
}

void
DatabaseConnection::makeReconnectCtl(const std::string& timer_name) {
    string type = "unknown";
    unsigned int retries = 0;
    unsigned int interval = 0;

    // Assumes that parsing ensures only valid values are present
    try {
        type = getParameter("type");
    } catch (...) {
        // Wasn't specified so we'll use default of "unknown".
    }

    std::string parm_str;
    try {
        parm_str = getParameter("max-reconnect-tries");
        retries = boost::lexical_cast<unsigned int>(parm_str);
    } catch (...) {
        // Wasn't specified so we'll use default of 0;
    }

    try {
        parm_str = getParameter("reconnect-wait-time");
        interval = boost::lexical_cast<unsigned int>(parm_str);
    } catch (...) {
        // Wasn't specified so we'll use default of 0;
    }

    OnFailAction action = OnFailAction::STOP_RETRY_EXIT;
    try {
        parm_str = getParameter("on-fail");
        action = ReconnectCtl::onFailActionFromText(parm_str);
    } catch (...) {
        // Wasn't specified so we'll use default of "stop-retry-exit";
    }

    reconnect_ctl_ = boost::make_shared<ReconnectCtl>(type, timer_name, retries,
                                                      interval, action);
}

bool
DatabaseConnection::invokeDbLostCallback(const ReconnectCtlPtr& db_reconnect_ctl) {
    if (DatabaseConnection::db_lost_callback_) {
        return (DatabaseConnection::db_lost_callback_(db_reconnect_ctl));
    }

    return (false);
}

bool
DatabaseConnection::invokeDbRecoveredCallback(const ReconnectCtlPtr& db_reconnect_ctl) {
    if (DatabaseConnection::db_recovered_callback_) {
        return (DatabaseConnection::db_recovered_callback_(db_reconnect_ctl));
    }

    return (false);
}

bool
DatabaseConnection::invokeDbFailedCallback(const ReconnectCtlPtr& db_reconnect_ctl) {
    if (DatabaseConnection::db_failed_callback_) {
        return (DatabaseConnection::db_failed_callback_(db_reconnect_ctl));
    }

    return (false);
}

isc::data::ElementPtr
DatabaseConnection::toElement(const ParameterMap& params) {
    isc::data::ElementPtr result = isc::data::Element::createMap();

    for (auto param: params) {
        std::string keyword = param.first;
        std::string value = param.second;

        if ((keyword == "lfc-interval") ||
            (keyword == "connect-timeout") ||
            (keyword == "reconnect-wait-time") ||
            (keyword == "max-reconnect-tries") ||
            (keyword == "port") ||
            (keyword == "max-row-errors")) {
            // integer parameters
            int64_t int_value;
            try {
                int_value = boost::lexical_cast<int64_t>(value);
                result->set(keyword, isc::data::Element::create(int_value));
            } catch (...) {
                LOG_ERROR(database_logger, DATABASE_TO_JSON_ERROR)
                    .arg("integer").arg(keyword).arg(value);
            }
        } else if ((keyword == "persist") ||
                   (keyword == "readonly")) {
            if (value == "true") {
                result->set(keyword, isc::data::Element::create(true));
            } else if (value == "false") {
                result->set(keyword, isc::data::Element::create(false));
            } else {
                LOG_ERROR(database_logger, DATABASE_TO_JSON_ERROR)
                    .arg("boolean").arg(keyword).arg(value);
            }
        } else if ((keyword == "type") ||
                   (keyword == "user") ||
                   (keyword == "password") ||
                   (keyword == "host") ||
                   (keyword == "name") ||
                   (keyword == "on-fail") ||
                   (keyword == "trust-anchor") ||
                   (keyword == "cert-file") ||
                   (keyword == "key-file") ||
                   (keyword == "cipher-list")) {
            result->set(keyword, isc::data::Element::create(value));
        } else {
            LOG_ERROR(database_logger, DATABASE_TO_JSON_ERROR)
                    .arg("unknown").arg(keyword).arg(value);
        }
    }

    return (result);
}

isc::data::ElementPtr
DatabaseConnection::toElementDbAccessString(const std::string& dbaccess) {
    ParameterMap params = parse(dbaccess);
    return (toElement(params));
}

DbCallback DatabaseConnection::db_lost_callback_ = 0;
DbCallback DatabaseConnection::db_recovered_callback_ = 0;
DbCallback DatabaseConnection::db_failed_callback_ = 0;

}  // namespace db
}  // namespace isc