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
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <legal_log_db_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <pgsql_legal_log.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dhcpsrv/timer_mgr.h>
#include <util/multi_threading_mgr.h>

#include <boost/static_assert.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <iomanip><--- 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 <sstream><--- 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.
#include <time.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc;
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::util;
using namespace std;

namespace {

/// @brief Catalog of all the SQL statements currently supported.  Note
/// that the order columns appear in statement body must match the order they
/// that the occur in the table.  This does not apply to the where clause.
PgSqlTaggedStatement tagged_statements[] = {
    // INSERT_LOG
    { 2, { OID_VARCHAR, OID_TEXT },
      "insert_log",
      "INSERT INTO logs(address, log) VALUES ($1, $2)"},

    // End of list sentinel
    { 0,  { 0 }, NULL, NULL}
};

};

namespace isc {
namespace legal_log {

/// @brief Supports exchanging log entries with PostgreSQL.
class PgSqlLegLExchange : public PgSqlExchange {
private:

    /// @brief Column numbers for each column in the logs table.
    /// These are used for both retrieving data and for looking up
    /// column labels for logging.  Note that their numeric order
    /// MUST match that of the column order in the logs table.
    static const size_t ADDRES_COL = 0;<--- class member 'PgSqlLegLExchange::ADDRES_COL' is never used.
    static const size_t LOG_COL = 1;<--- class member 'PgSqlLegLExchange::LOG_COL' is never used.
    /// @brief Number of columns in the table holding log entries.
    static const size_t LOG_COLUMNS = 2;

public:
    /// @brief Constructor
    PgSqlLegLExchange() : address_(""), log_("") {

        BOOST_STATIC_ASSERT(0 < LOG_COLUMNS);

        // Set the column names (for error messages)
        columns_.push_back("log");
    }

    /// @brief Destructor
    ~PgSqlLegLExchange() = default;

    /// @brief Creates the bind array for sending log text to the database.
    ///
    /// Converts each member into the appropriate form and adds it
    /// to the bind array.  Note that the array additions must occur in the
    /// order the columns are specified in the SQL statement.  By convention
    /// all columns in the table are explicitly listed in the SQL statement(s)
    /// in the same order as they occur in the table.
    ///
    /// @param address address or prefix of the log entry
    /// @param log log entry that is to be written to the database
    /// @param[out] bind_array array to populate with the values
    ///
    /// @throw DbOperationError if bind_array cannot be populated.
    void createBindForSend(const string& address, const string& log,
                           PsqlBindArray& bind_array) {
        if (log.empty()) {
            isc_throw(BadValue, "createBindForSend:: log entry is NULL");
        }

        // Store arguments to ensure they remain valid.
        address_ = address;
        log_ = log;

        try {
            bind_array.add(address_);
            bind_array.add(log_);
        } catch (const std::exception& ex) {
            isc_throw(DbOperationError,
                      "Could not create bind array from log: '"
                      << log_ << "', reason: " << ex.what());
        }
    }

private:
    /// @brief Members used for binding and conversion
    //@{
    string address_;
    string log_;
    //@}
};

// PgSqlStoreContext Constructor

PgSqlStoreContext::PgSqlStoreContext(const DatabaseConnection::ParameterMap& parameters,
                                     IOServiceAccessorPtr io_service_accessor,
                                     db::DbCallback db_reconnect_callback)
    : conn_(parameters, io_service_accessor, db_reconnect_callback) {
}

// PgSqlStoreContextAlloc Constructor and Destructor

PgSqlStore::PgSqlStoreContextAlloc::PgSqlStoreContextAlloc(const PgSqlStore& store)
    : ctx_(), store_(store) {

    if (MultiThreadingMgr::instance().getMode()) {
        // multi-threaded
        {
            // we need to protect the whole pool_ operation, hence extra scope {}
            lock_guard<mutex> lock(store_.pool_->mutex_);
            if (!store_.pool_->pool_.empty()) {
                ctx_ = store_.pool_->pool_.back();
                store_.pool_->pool_.pop_back();
            }
        }
        if (!ctx_) {
            ctx_ = store_.createContext();
        }
    } else {
        // single-threaded
        if (store_.pool_->pool_.empty()) {
            isc_throw(Unexpected, "No available PostgreSQL store context?!");
        }
        ctx_ = store_.pool_->pool_.back();
    }
}

PgSqlStore::PgSqlStoreContextAlloc::~PgSqlStoreContextAlloc() {
    if (MultiThreadingMgr::instance().getMode()) {
        // multi-threaded
        lock_guard<mutex> lock(store_.pool_->mutex_);
        store_.pool_->pool_.push_back(ctx_);
    }
    // If running in single-threaded mode, there's nothing to do here.
}

// PgSqlStore

PgSqlStore::PgSqlStore(const DatabaseConnection::ParameterMap& parameters)
    : timer_name_("") {

    // Store connection parameters.
    BackendStore::setParameters(parameters);

    // Create unique timer name per instance.
    timer_name_ = "PgSqlLegalStore[";
    timer_name_ += boost::lexical_cast<std::string>(reinterpret_cast<uint64_t>(this));
    timer_name_ += "]DbReconnectTimer";
}

void PgSqlStore::open() {
    LegalLogDbLogger pushed;

    // Check TLS support.
    size_t tls(0);
    auto const& parameters = BackendStore::getParameters();
    tls += parameters.count("trust-anchor");
    tls += parameters.count("cert-file");
    tls += parameters.count("key-file");
    tls += parameters.count("cipher-list");
#ifdef HAVE_PGSQL_SSL
    if ((tls > 0) && !PgSqlConnection::warned_about_tls) {
        PgSqlConnection::warned_about_tls = true;
        LOG_INFO(legal_log_logger, LEGAL_LOG_PGSQL_TLS_SUPPORT)
            .arg(DatabaseConnection::redactedAccessString(parameters));
        PQinitSSL(1);
    }
#else
    if (tls > 0) {
        LOG_ERROR(legal_log_logger, LEGAL_LOG_PGSQL_NO_TLS_SUPPORT)
            .arg(DatabaseConnection::redactedAccessString(parameters));
        isc_throw(DbOpenError, "Attempt to configure TLS for PostgreSQL "
                  << "backend (built with this feature disabled)");
    }
#endif

    // Test schema version first.
    pair<uint32_t, uint32_t> code_version(PGSQL_SCHEMA_VERSION_MAJOR,
                                          PGSQL_SCHEMA_VERSION_MINOR);

    BackendStorePtr store = BackendStore::instance();
    BackendStore::instance().reset();

    string timer_name;
    bool retry = false;
    if (BackendStore::getParameters().count("retry-on-startup")) {
        if (BackendStore::getParameters().at("retry-on-startup") == "true") {
            retry = true;
        }
    }
    if (retry) {
        timer_name = timer_name_;
    }

    pair<uint32_t, uint32_t> db_version = getVersion(timer_name);
    if (code_version != db_version) {
        isc_throw(DbOpenError,
                  "PostgreSQL schema version mismatch: need version: "
                  << code_version.first << "." << code_version.second
                  << " found version:  " << db_version.first << "."
                  << db_version.second);
    }

    BackendStore::instance() = store;

    // Create an initial context.
    pool_.reset(new PgSqlStoreContextPool());
    pool_->pool_.push_back(createContext());
}

// Create context.

PgSqlStoreContextPtr
PgSqlStore::createContext() const {
    PgSqlStoreContextPtr ctx(new PgSqlStoreContext(BackendStore::getParameters(),
        IOServiceAccessorPtr(new IOServiceAccessor(&PgSqlStore::getIOService)),
        &PgSqlStore::dbReconnect));

    // Open the database.
    ctx->conn_.openDatabase();

    size_t i = 0;
    for (; tagged_statements[i].text != NULL; ++i) {
        ctx->conn_.prepareStatement(tagged_statements[i]);
    }

    // Just in case somebody fubared things
    if (i != NUM_STATEMENTS) {
        isc_throw(DbOpenError, "Number of statements prepared: " << i
                  << " does not match expected count:" << NUM_STATEMENTS);
    }

    // Create the exchange objects for use in exchanging data between the
    // program and the database.
    ctx->exchange_.reset(new PgSqlLegLExchange());

    // Create ReconnectCtl for this connection.
    ctx->conn_.makeReconnectCtl(timer_name_);

    return (ctx);
}

PgSqlStore::~PgSqlStore() {
}

void PgSqlStore::close() {
}

void
PgSqlStore::writeln(const string& text, const std::string& addr) {
    if (text.empty()) {
        return;
    }

    LOG_DEBUG(legal_log_logger, DB_DBG_TRACE_DETAIL,
              LEGAL_LOG_PGSQL_INSERT_LOG).arg(text);

    LegalLogDbLogger pushed;

    // Get a context
    PgSqlStoreContextAlloc get_context(*this);
    PgSqlStoreContextPtr ctx = get_context.ctx_;

    PsqlBindArray bind_array;
    ctx->exchange_->createBindForSend(addr, text, bind_array);

    PgSqlResult r(PQexecPrepared(ctx->conn_,
                                 tagged_statements[INSERT_LOG].name,
                                 tagged_statements[INSERT_LOG].nbparams,
                                 &bind_array.values_[0],
                                 &bind_array.lengths_[0],
                                 &bind_array.formats_[0], 0));

    int s = PQresultStatus(r);

    if (s != PGRES_COMMAND_OK) {
        ctx->conn_.checkStatementError(r, tagged_statements[INSERT_LOG]);
    }
}

pair<uint32_t, uint32_t>
PgSqlStore::getVersion(const std::string& timer_name) const {
    LOG_DEBUG(legal_log_logger, DB_DBG_TRACE_DETAIL,
              LEGAL_LOG_PGSQL_GET_VERSION);

    LegalLogDbLogger pushed;

    IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService));
    DbCallback cb(&PgSqlStore::dbReconnect);

    return (PgSqlConnection::getVersion(BackendStore::getParameters(), ac, cb, timer_name));
}

bool
PgSqlStore::dbReconnect(ReconnectCtlPtr db_reconnect_ctl) {
    MultiThreadingCriticalSection cs;

    // Invoke application layer connection lost callback.
    if (!DatabaseConnection::invokeDbLostCallback(db_reconnect_ctl)) {
        return (false);
    }

    bool reopened = false;

    const std::string timer_name = db_reconnect_ctl->timerName();

    // At least one connection was lost.
    try {
        auto parameters = BackendStore::getParameters();
        // Set legal store to NULL
        BackendStore::instance().reset();
        // Create temporary legal store
        BackendStorePtr store(new PgSqlStore(parameters));
        store->open();
        // If everything is fine, set the legal store
        BackendStore::instance() = store;
        BackendStore::parseExtraParameters(BackendStore::getConfig());
        reopened = true;
    } catch (const std::exception& ex) {
        LOG_ERROR(legal_log_logger, LEGAL_LOG_PGSQL_DB_RECONNECT_ATTEMPT_FAILED)
                .arg(ex.what());
    }

    if (reopened) {
        // Cancel the timer.
        if (TimerMgr::instance()->isTimerRegistered(timer_name)) {
            TimerMgr::instance()->unregisterTimer(timer_name);
        }

        // Invoke application layer connection recovered callback.
        if (!DatabaseConnection::invokeDbRecoveredCallback(db_reconnect_ctl)) {
            return (false);
        }
    } else {
        if (!db_reconnect_ctl->checkRetries()) {
            // We're out of retries, log it and initiate shutdown.
            LOG_ERROR(legal_log_logger, LEGAL_LOG_PGSQL_DB_RECONNECT_FAILED)
                    .arg(db_reconnect_ctl->maxRetries());

            // Cancel the timer.
            if (TimerMgr::instance()->isTimerRegistered(timer_name)) {
                TimerMgr::instance()->unregisterTimer(timer_name);
            }

            // Invoke application layer connection failed callback.
            DatabaseConnection::invokeDbFailedCallback(db_reconnect_ctl);
            return (false);
        }

        LOG_INFO(legal_log_logger, LEGAL_LOG_PGSQL_DB_RECONNECT_ATTEMPT_SCHEDULE)
                .arg(db_reconnect_ctl->maxRetries() - db_reconnect_ctl->retriesLeft() + 1)
                .arg(db_reconnect_ctl->maxRetries())
                .arg(db_reconnect_ctl->retryInterval());

        // Start the timer.
        if (!TimerMgr::instance()->isTimerRegistered(timer_name)) {
            TimerMgr::instance()->registerTimer(timer_name,
                std::bind(&PgSqlStore::dbReconnect, db_reconnect_ctl),
                          db_reconnect_ctl->retryInterval(),
                          asiolink::IntervalTimer::ONE_SHOT);
        }
        TimerMgr::instance()->setup(timer_name);
    }

    return (true);
}

} // end of isc::legal_log namespace
} // end of isc namespace