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
// 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 <cc/command_interpreter.h>
#include <config/base_command_mgr.h>
#include <config/config_log.h>
#include <cryptolink/crypto_hash.h>
#include <hooks/callout_handle.h>
#include <hooks/hooks_manager.h>
#include <util/encode/encode.h>
#include <util/buffer.h>
#include <functional><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::data;
using namespace isc::hooks;
namespace ph = std::placeholders;

namespace {

/// Structure that holds registered hook indexes
struct BaseCommandMgrHooks {
    int hook_index_command_processed_; ///< index for "command_processe" hook point

    /// Constructor that registers hook points for AllocationEngine
    BaseCommandMgrHooks() {
        hook_index_command_processed_ = HooksManager::registerHook("command_processed");
    }
};

// Declare a Hooks object. As this is outside any function or method, it
// will be instantiated (and the constructor run) when the module is loaded.
// As a result, the hook indexes will be defined before any method in this
// module is called.
BaseCommandMgrHooks Hooks;

}; // anonymous namespace

namespace isc {
namespace config {

BaseCommandMgr::BaseCommandMgr() {
    registerCommand("list-commands", std::bind(&BaseCommandMgr::listCommandsHandler,
                                               this, ph::_1, ph::_2));
}

void
BaseCommandMgr::registerCommand(const std::string& cmd, CommandHandler handler) {
    if (!handler) {
        isc_throw(InvalidCommandHandler, "Specified command handler is NULL");
    }

    HandlerContainer::const_iterator it = handlers_.find(cmd);
    if (it != handlers_.end()) {
        isc_throw(InvalidCommandName, "Handler for command '" << cmd
                  << "' is already installed.");
    }

    HandlersPair handlers;
    handlers.handler = handler;
    handlers_.insert(make_pair(cmd, handlers));

    LOG_DEBUG(command_logger, DBG_COMMAND, COMMAND_REGISTERED).arg(cmd);
}

void
BaseCommandMgr::registerExtendedCommand(const std::string& cmd,
                                        ExtendedCommandHandler handler) {
    if (!handler) {
        isc_throw(InvalidCommandHandler, "Specified command handler is NULL");
    }

    HandlerContainer::const_iterator it = handlers_.find(cmd);
    if (it != handlers_.end()) {
        isc_throw(InvalidCommandName, "Handler for command '" << cmd
                  << "' is already installed.");
    }

    HandlersPair handlers;
    handlers.extended_handler = handler;
    handlers_.insert(make_pair(cmd, handlers));

    LOG_DEBUG(command_logger, DBG_COMMAND, COMMAND_EXTENDED_REGISTERED).arg(cmd);
}

void
BaseCommandMgr::deregisterCommand(const std::string& cmd) {
    if (cmd == "list-commands") {
        isc_throw(InvalidCommandName,
                  "Can't uninstall internal command 'list-commands'");
    }

    HandlerContainer::iterator it = handlers_.find(cmd);
    if (it == handlers_.end()) {
        isc_throw(InvalidCommandName, "Handler for command '" << cmd
                  << "' not found.");
    }
    handlers_.erase(it);

    LOG_DEBUG(command_logger, DBG_COMMAND, COMMAND_DEREGISTERED).arg(cmd);
}

void
BaseCommandMgr::deregisterAll() {

    // No need to log anything here. deregisterAll is not used in production
    // code, just in tests.
    handlers_.clear();
    registerCommand("list-commands",
        std::bind(&BaseCommandMgr::listCommandsHandler, this, ph::_1, ph::_2));
}

isc::data::ConstElementPtr
BaseCommandMgr::processCommand(const isc::data::ConstElementPtr& cmd) {
    if (!cmd) {
        return (createAnswer(CONTROL_RESULT_ERROR,
                             "Command processing failed: NULL command parameter"));
    }

    try {
        ConstElementPtr arg;
        std::string name = parseCommand(arg, cmd);

        LOG_INFO(command_logger, COMMAND_RECEIVED).arg(name);

        ConstElementPtr response = handleCommand(name, arg, cmd);

        // If there any callouts for command-processed hook point call them
        if (HooksManager::calloutsPresent(Hooks.hook_index_command_processed_)) {
            // Commands are not associated with anything so there's no pre-existing
            // callout.
            CalloutHandlePtr callout_handle = HooksManager::createCalloutHandle();

            // Add the command name, arguments, and response to the callout context
            callout_handle->setArgument("name", name);
            callout_handle->setArgument("arguments", arg);
            callout_handle->setArgument("response", response);

            // Call callouts
            HooksManager::callCallouts(Hooks.hook_index_command_processed_,
                                        *callout_handle);

            // Refresh the response from the callout context in case it was modified.
            // @todo Should we allow this?
            callout_handle->getArgument("response", response);
        }

        return (response);

    } catch (const Exception& e) {
        LOG_WARN(command_logger, COMMAND_PROCESS_ERROR2).arg(e.what());
        return (createAnswer(CONTROL_RESULT_ERROR,
                             std::string("Error during command processing: ")
                             + e.what()));
    }
}

ConstElementPtr
BaseCommandMgr::handleCommand(const std::string& cmd_name,
                              const ConstElementPtr& params,
                              const ConstElementPtr& original_cmd) {
    auto it = handlers_.find(cmd_name);
    if (it == handlers_.end()) {
        // Ok, there's no such command.
        return (createAnswer(CONTROL_RESULT_COMMAND_UNSUPPORTED,
                             "'" + cmd_name + "' command not supported."));
    }

    // Call the actual handler and return whatever it returned
    if (it->second.handler) {
        return (it->second.handler(cmd_name, params));
    }
    return (it->second.extended_handler(cmd_name, params, original_cmd));
}

isc::data::ConstElementPtr
BaseCommandMgr::listCommandsHandler(const std::string& /* name */,
                                    const isc::data::ConstElementPtr& ) {
    using namespace isc::data;
    ElementPtr commands = Element::createList();
    for (auto const& it : handlers_) {
        commands->add(Element::create(it.first));
    }
    return (createAnswer(CONTROL_RESULT_SUCCESS, commands));
}

std::string
BaseCommandMgr::getHash(const isc::data::ConstElementPtr& config) {

    // Sanity.
    if (!config) {
        isc_throw(Unexpected, "BaseCommandMgr::getHash called with null");
    }

    // First, get the string representation.
    std::string config_txt = config->str();
    isc::util::OutputBuffer hash_data(0);
    isc::cryptolink::digest(config_txt.c_str(),
                            config_txt.size(),
                            isc::cryptolink::HashAlgorithm::SHA256,
                            hash_data);

    // Now we need to convert this to output buffer to vector, which can be accepted
    // by encodeHex().
    std::vector<uint8_t> hash;
    hash.resize(hash_data.getLength());
    if (hash.size() > 0) {
        memmove(&hash[0], hash_data.getData(), hash.size());
    }

    // Now encode the value as base64 and we're done here.
    return (isc::util::encode::encodeHex(hash));
}

} // namespace isc::config
} // namespace isc