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
// 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 <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mysql.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mysql/testutils/mysql_schema.h>
#include <mysql/mysql_connection.h>
#include <exceptions/exceptions.h>

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

using namespace std;

namespace isc {
namespace db {
namespace test {

const char* MYSQL_VALID_TYPE = "type=mysql";

string
validMySQLConnectionString() {
    return (connectionString(MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST,
                             VALID_USER, VALID_PASSWORD));
}

void destroyMySQLSchema(bool show_err, bool force) {
    // If force is true or wipeMySQLData() fails, destroy the schema.
    if (force || (!softWipeEnabled()) || wipeMySQLData(show_err)) {
        runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_drop.mysql", show_err);
    }
}

void createMySQLSchema(bool show_err, bool force) {
    // If force is true or wipeMySQLData() fails, recreate the schema.
    if (force || (!softWipeEnabled()) || wipeMySQLData(show_err)) {
        destroyMySQLSchema(show_err, true);
        runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_create.mysql", show_err);
    }
}

bool wipeMySQLData(bool show_err) {
    std::ostringstream cmd;
    cmd << "sh " << DATABASE_WIPE_DIR << "/";

    std::ostringstream version;
    version << MYSQL_SCHEMA_VERSION_MAJOR  << "." << MYSQL_SCHEMA_VERSION_MINOR;

    cmd << "mysql/wipe_data.sh " << version.str()
        << " -N -B --user=keatest --password=keatest keatest";
    if (!show_err) {
        cmd << " 2>/dev/null ";
    }

    int retval = ::system(cmd.str().c_str());
    if (retval) {
        std::cerr << "wipeMySQLData failed:[" << cmd.str() << "]" << std::endl;
    }

    return(retval);
}

void runMySQLScript(const std::string& path, const std::string& script_name,
                    bool show_err) {
    std::ostringstream cmd;
    cmd << "mysql -N -B --user=keatest --password=keatest keatest";
    if (!show_err) {
        cmd << " 2>/dev/null ";
    }

    if (!path.empty()) {
        cmd << " < " << path << "/";
    }

    cmd << script_name;

    int retval = ::system(cmd.str().c_str());
    if (retval) {
        std::cerr << "runMySQLSchema failed: " << cmd.str() << std::endl;
        isc_throw(Unexpected, "runMySQLSchema failed: " << cmd.str());
    }
}

string getMySQLTlsEnv() {
    const string name("KEA_MYSQL_HAVE_SSL");
    const char* val = getenv(name.c_str());
    return (val ? string(val) : "");
}

string getMySQLTlsServerVariable(string variable) {<--- Function parameter 'variable' should be passed by const reference.
    MYSQL_RES* result(0);
    try {
        DatabaseConnection::ParameterMap parameters =
            DatabaseConnection::parse(validMySQLConnectionString());
        MySqlConnection conn(parameters);
        conn.openDatabase();
        string sql("SHOW GLOBAL VARIABLES LIKE '");
        sql += variable;
        sql += "'";
        if (mysql_query(conn.mysql_, sql.c_str())) {
            isc_throw(DbOperationError,
                      sql << ": " << mysql_error(conn.mysql_));
        }
        result = mysql_use_result(conn.mysql_);
        size_t count = mysql_num_fields(result);
        if (count != 2) {
            isc_throw(DbOperationError,
                      sql << " returned " << count << " rows, expecting 2");
        }
        MYSQL_ROW row = mysql_fetch_row(result);
        if (!row) {
            isc_throw(DbOperationError, sql << " returned row is null");
        }
        // first column is 'have_ssl', second is the status.
        string name(row[0]);
        if (name != variable) {
            isc_throw(DbOperationError,
                      sql << " returned a wrong name '" << name
                      << "', expected '" << variable << "'");
        }
        string value(row[1]);
        mysql_free_result(result);
        return (value);
    } catch (...) {
        if (result) {
            mysql_free_result(result);
        }
        throw;
    }
}

bool isMySQLTlsConfigured() {
    if (getMySQLTlsServerVariable("ssl_ca").find("kea-ca.crt") == string::npos) {
        return (false);
    }
    if (getMySQLTlsServerVariable("ssl_cert").find("kea-server.crt") == string::npos) {
        return (false);
    }
    if (getMySQLTlsServerVariable("ssl_key").find("kea-server.key") == string::npos) {
        return (false);
    }
    return (true);
}

string getMySQLTlsServer() {
    string value = getMySQLTlsServerVariable("have_ssl");
    if (value == "YES" && !isMySQLTlsConfigured()) {
        value = "UNCONFIGURED";
    }
    const string env("KEA_MYSQL_HAVE_SSL");
    static_cast<void>(setenv(env.c_str(), value.c_str(), 1));
    return (value);
}

}  // namespace test
}  // namespace db
}  // namespace isc