| 1 | ///////////////////////////////////////////////////////////////////////////
|
|---|
| 2 | // NSClient++ Base Service
|
|---|
| 3 | //
|
|---|
| 4 | // Copyright (c) 2004 MySolutions NORDIC (http://www.medin.name)
|
|---|
| 5 | //
|
|---|
| 6 | // Date: 2004-03-13
|
|---|
| 7 | // Author: Michael Medin (michael@medin.name)
|
|---|
| 8 | //
|
|---|
| 9 | // Part of this file is based on work by Bruno Vais (bvais@usa.net)
|
|---|
| 10 | //
|
|---|
| 11 | // This software is provided "AS IS", without a warranty of any kind.
|
|---|
| 12 | // You are free to use/modify this code but leave this header intact.
|
|---|
| 13 | //
|
|---|
| 14 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 15 | #include "StdAfx.h"
|
|---|
| 16 | #include "NSClient++.h"
|
|---|
| 17 | #include "core_api.h"
|
|---|
| 18 | #include <charEx.h>
|
|---|
| 19 | #include <arrayBuffer.h>
|
|---|
| 20 | #include <settings/settings_core.hpp>
|
|---|
| 21 | #include "../helpers/settings_manager/settings_manager_impl.h"
|
|---|
| 22 | #include <nscapi/nscapi_helper.hpp>
|
|---|
| 23 | #ifdef _WIN32
|
|---|
| 24 | #include <ServiceCmd.h>
|
|---|
| 25 | #endif
|
|---|
| 26 | #include <nsclient/logger.hpp>
|
|---|
| 27 |
|
|---|
| 28 | using namespace nscp::helpers;
|
|---|
| 29 |
|
|---|
| 30 | #define LOG_ERROR_STD(msg) LOG_ERROR(((std::wstring)msg).c_str())
|
|---|
| 31 | #define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::wstring)msg).c_str())
|
|---|
| 32 | #define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::wstring)msg).c_str())
|
|---|
| 33 | #define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::wstring)msg).c_str())
|
|---|
| 34 |
|
|---|
| 35 | #define LOG_ERROR(msg) { nsclient::logging::logger::get_logger()->error(_T("core"), __FILE__, __LINE__, msg); }
|
|---|
| 36 | #define LOG_CRITICAL(msg) { nsclient::logging::logger::get_logger()->error(_T("core"), __FILE__, __LINE__, msg); }
|
|---|
| 37 | #define LOG_MESSAGE(msg) { nsclient::logging::logger::get_logger()->info(_T("core"), __FILE__, __LINE__, msg); }
|
|---|
| 38 | #define LOG_DEBUG(msg) { nsclient::logging::logger::get_logger()->debug(_T("core"), __FILE__, __LINE__, msg); }
|
|---|
| 39 |
|
|---|
| 40 | NSCAPI::errorReturn NSAPIExpandPath(const wchar_t* key, wchar_t* buffer,unsigned int bufLen) {
|
|---|
| 41 | try {
|
|---|
| 42 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, mainClient.expand_path(key), NSCAPI::isSuccess);
|
|---|
| 43 | } catch (...) {
|
|---|
| 44 | LOG_ERROR_STD(_T("Failed to getString: ") + key);
|
|---|
| 45 | return NSCAPI::hasFailed;
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | NSCAPI::errorReturn NSAPIGetSettingsString(const wchar_t* section, const wchar_t* key, const wchar_t* defaultValue, wchar_t* buffer, unsigned int bufLen) {
|
|---|
| 50 | try {
|
|---|
| 51 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, settings_manager::get_settings()->get_string(section, key, defaultValue), NSCAPI::isSuccess);
|
|---|
| 52 | } catch (settings::settings_exception e) {
|
|---|
| 53 | LOG_ERROR_STD(_T("Failed to get string: ") + e.getMessage());
|
|---|
| 54 | return NSCAPI::hasFailed;
|
|---|
| 55 | } catch (const std::exception &e) {
|
|---|
| 56 | LOG_ERROR_STD(_T("Failed to get string: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 57 | return NSCAPI::hasFailed;
|
|---|
| 58 | } catch (...) {
|
|---|
| 59 | LOG_ERROR_STD(_T("Failed to get string: <UNKNOWN EXCEPTION>"));
|
|---|
| 60 | return NSCAPI::hasFailed;
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | int NSAPIGetSettingsInt(const wchar_t* section, const wchar_t* key, int defaultValue) {
|
|---|
| 64 | try {
|
|---|
| 65 | return settings_manager::get_settings()->get_int(section, key, defaultValue);
|
|---|
| 66 | } catch (settings::settings_exception e) {
|
|---|
| 67 | LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage());
|
|---|
| 68 | return defaultValue;
|
|---|
| 69 | } catch (const std::exception &e) {
|
|---|
| 70 | LOG_ERROR_STD(_T("Failed to get key: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 71 | return defaultValue;
|
|---|
| 72 | } catch (...) {
|
|---|
| 73 | LOG_ERROR_STD(_T("Failed to get key: <UNKNOWN EXCEPTION>"));
|
|---|
| 74 | return defaultValue;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | int NSAPIGetSettingsBool(const wchar_t* section, const wchar_t* key, int defaultValue) {
|
|---|
| 78 | try {
|
|---|
| 79 | return settings_manager::get_settings()->get_bool(section, key, defaultValue==1);
|
|---|
| 80 | } catch (settings::settings_exception e) {
|
|---|
| 81 | LOG_ERROR_STD(_T("Failed to get key: ") + e.getMessage());
|
|---|
| 82 | return defaultValue;
|
|---|
| 83 | } catch (const std::exception &e) {
|
|---|
| 84 | LOG_ERROR_STD(_T("Failed to get key: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 85 | return defaultValue;
|
|---|
| 86 | } catch (...) {
|
|---|
| 87 | LOG_ERROR_STD(_T("Failed to get key: <UNKNOWN EXCEPTION>"));
|
|---|
| 88 | return defaultValue;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | NSCAPI::errorReturn NSAPIGetBasePath(wchar_t*buffer, unsigned int bufLen) {
|
|---|
| 92 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, mainClient.getBasePath().string(), NSCAPI::isSuccess);
|
|---|
| 93 | }
|
|---|
| 94 | NSCAPI::errorReturn NSAPIGetApplicationName(wchar_t*buffer, unsigned int bufLen) {
|
|---|
| 95 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, APPLICATION_NAME, NSCAPI::isSuccess);
|
|---|
| 96 | }
|
|---|
| 97 | NSCAPI::errorReturn NSAPIGetApplicationVersionStr(wchar_t*buffer, unsigned int bufLen) {
|
|---|
| 98 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, CURRENT_SERVICE_VERSION, NSCAPI::isSuccess);
|
|---|
| 99 | }
|
|---|
| 100 | void NSAPISimpleMessage(const wchar_t* module, int loglevel, const char* file, int line, const wchar_t* message) {
|
|---|
| 101 | nsclient::logging::logger::get_logger()->log(module, loglevel, file, line, message);
|
|---|
| 102 | }
|
|---|
| 103 | void NSAPIMessage(const char* data, unsigned int count) {
|
|---|
| 104 | std::string message(data, count);
|
|---|
| 105 | nsclient::logging::logger::get_logger()->raw(message);
|
|---|
| 106 | }
|
|---|
| 107 | void NSAPIStopServer(void) {
|
|---|
| 108 | mainClient.get_service_control().stop();
|
|---|
| 109 | }
|
|---|
| 110 | NSCAPI::nagiosReturn NSAPIInject(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) {
|
|---|
| 111 | std::string request (request_buffer, request_buffer_len), response;
|
|---|
| 112 | NSCAPI::nagiosReturn ret = mainClient.injectRAW(command, request, response);
|
|---|
| 113 | *response_buffer_len = response.size();
|
|---|
| 114 | if (response.empty())
|
|---|
| 115 | *response_buffer = NULL;
|
|---|
| 116 | else {
|
|---|
| 117 | *response_buffer = new char[*response_buffer_len + 10];
|
|---|
| 118 | memcpy(*response_buffer, response.c_str(), *response_buffer_len);
|
|---|
| 119 | }
|
|---|
| 120 | return ret;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | NSCAPI::nagiosReturn NSAPIExecCommand(const wchar_t* target, const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) {
|
|---|
| 124 | std::string request (request_buffer, request_buffer_len), response;
|
|---|
| 125 | NSCAPI::nagiosReturn ret = mainClient.exec_command(target, command, request, response);
|
|---|
| 126 | *response_buffer_len = response.size();
|
|---|
| 127 | if (response.empty())
|
|---|
| 128 | *response_buffer = NULL;
|
|---|
| 129 | else {
|
|---|
| 130 | *response_buffer = new char[*response_buffer_len + 10];
|
|---|
| 131 | memcpy(*response_buffer, response.c_str(), *response_buffer_len);
|
|---|
| 132 | }
|
|---|
| 133 | return ret;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | NSCAPI::errorReturn NSAPIGetSettingsSection(const wchar_t* section, wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 139 | try {
|
|---|
| 140 | unsigned int len = 0;
|
|---|
| 141 | *aBuffer = array_buffer::list2arrayBuffer(settings_manager::get_settings()->get_keys(section), len);
|
|---|
| 142 | *bufLen = len;
|
|---|
| 143 | return NSCAPI::isSuccess;
|
|---|
| 144 | } catch (settings::settings_exception e) {
|
|---|
| 145 | LOG_ERROR_STD(_T("Failed to get section: ") + e.getMessage());
|
|---|
| 146 | } catch (...) {
|
|---|
| 147 | LOG_ERROR_STD(_T("Failed to getSection: ") + section);
|
|---|
| 148 | }
|
|---|
| 149 | return NSCAPI::hasFailed;
|
|---|
| 150 | }
|
|---|
| 151 | NSCAPI::errorReturn NSAPIGetSettingsSections(const wchar_t* section, wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 152 | try {
|
|---|
| 153 | unsigned int len = 0;
|
|---|
| 154 | *aBuffer = array_buffer::list2arrayBuffer(settings_manager::get_settings()->get_sections(section), len);
|
|---|
| 155 | *bufLen = len;
|
|---|
| 156 | return NSCAPI::isSuccess;
|
|---|
| 157 | } catch (settings::settings_exception e) {
|
|---|
| 158 | LOG_ERROR_STD(_T("Failed to get section: ") + e.getMessage());
|
|---|
| 159 | } catch (...) {
|
|---|
| 160 | LOG_ERROR_STD(_T("Failed to getSection: ") + section);
|
|---|
| 161 | }
|
|---|
| 162 | return NSCAPI::hasFailed;
|
|---|
| 163 | }
|
|---|
| 164 | NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 165 | array_buffer::destroyArrayBuffer(*aBuffer, *bufLen);
|
|---|
| 166 | *bufLen = 0;
|
|---|
| 167 | *aBuffer = NULL;
|
|---|
| 168 | return NSCAPI::isSuccess;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | NSCAPI::boolReturn NSAPICheckLogMessages(int messageType) {
|
|---|
| 172 | return nsclient::logging::logger::get_logger()->should_log(messageType);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | NSCAPI::errorReturn NSAPIEncrypt(unsigned int algorithm, const wchar_t* inBuffer, unsigned int inBufLen, wchar_t* outBuf, unsigned int *outBufLen) {
|
|---|
| 176 | if (algorithm != NSCAPI::encryption_xor) {
|
|---|
| 177 | LOG_ERROR(_T("Unknown algortihm requested."));
|
|---|
| 178 | return NSCAPI::hasFailed;
|
|---|
| 179 | }
|
|---|
| 180 | /*
|
|---|
| 181 | TODO reimplement this
|
|---|
| 182 |
|
|---|
| 183 | std::wstring key = settings_manager::get_settings()->get_string(SETTINGS_KEY(protocol_def::MASTER_KEY));
|
|---|
| 184 | int tcharInBufLen = 0;
|
|---|
| 185 | char *c = charEx::tchar_to_char(inBuffer, inBufLen, tcharInBufLen);
|
|---|
| 186 | std::wstring::size_type j=0;
|
|---|
| 187 | for (int i=0;i<tcharInBufLen;i++,j++) {
|
|---|
| 188 | if (j > key.size())
|
|---|
| 189 | j = 0;
|
|---|
| 190 | c[i] ^= key[j];
|
|---|
| 191 | }
|
|---|
| 192 | size_t cOutBufLen = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, NULL, NULL);
|
|---|
| 193 | if (!outBuf) {
|
|---|
| 194 | *outBufLen = static_cast<unsigned int>(cOutBufLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work
|
|---|
| 195 | return NSCAPI::isSuccess;
|
|---|
| 196 | }
|
|---|
| 197 | char *cOutBuf = new char[cOutBufLen+1];
|
|---|
| 198 | size_t len = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, cOutBuf, cOutBufLen);
|
|---|
| 199 | delete [] c;
|
|---|
| 200 | if (len == 0) {
|
|---|
| 201 | LOG_ERROR(_T("Invalid out buffer length."));
|
|---|
| 202 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 203 | }
|
|---|
| 204 | int realOutLen;
|
|---|
| 205 | wchar_t *realOut = charEx::char_to_tchar(cOutBuf, cOutBufLen, realOutLen);
|
|---|
| 206 | if (static_cast<unsigned int>(realOutLen) >= *outBufLen) {
|
|---|
| 207 | LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated."));
|
|---|
| 208 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 209 | }
|
|---|
| 210 | wcsncpy(outBuf, *outBufLen, realOut);
|
|---|
| 211 | delete [] realOut;
|
|---|
| 212 | outBuf[realOutLen] = 0;
|
|---|
| 213 | *outBufLen = static_cast<unsigned int>(realOutLen);
|
|---|
| 214 | */
|
|---|
| 215 | return NSCAPI::isSuccess;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | NSCAPI::errorReturn NSAPIDecrypt(unsigned int algorithm, const wchar_t* inBuffer, unsigned int inBufLen, wchar_t* outBuf, unsigned int *outBufLen) {
|
|---|
| 219 | if (algorithm != NSCAPI::encryption_xor) {
|
|---|
| 220 | LOG_ERROR(_T("Unknown algortihm requested."));
|
|---|
| 221 | return NSCAPI::hasFailed;
|
|---|
| 222 | }
|
|---|
| 223 | /*
|
|---|
| 224 | int inBufLenC = 0;
|
|---|
| 225 | char *inBufferC = charEx::tchar_to_char(inBuffer, inBufLen, inBufLenC);
|
|---|
| 226 | size_t cOutLen = b64::b64_decode(inBufferC, inBufLenC, NULL, NULL);
|
|---|
| 227 | if (!outBuf) {
|
|---|
| 228 | *outBufLen = static_cast<unsigned int>(cOutLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work
|
|---|
| 229 | return NSCAPI::isSuccess;
|
|---|
| 230 | }
|
|---|
| 231 | char *cOutBuf = new char[cOutLen+1];
|
|---|
| 232 | size_t len = b64::b64_decode(inBufferC, inBufLenC, reinterpret_cast<void*>(cOutBuf), cOutLen);
|
|---|
| 233 | delete [] inBufferC;
|
|---|
| 234 | if (len == 0) {
|
|---|
| 235 | LOG_ERROR(_T("Invalid out buffer length."));
|
|---|
| 236 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 237 | }
|
|---|
| 238 | int realOutLen;
|
|---|
| 239 |
|
|---|
| 240 | std::wstring key = settings_manager::get_settings()->get_string(SETTINGS_KEY(protocol_def::MASTER_KEY));
|
|---|
| 241 | std::wstring::size_type j=0;
|
|---|
| 242 | for (int i=0;i<cOutLen;i++,j++) {
|
|---|
| 243 | if (j > key.size())
|
|---|
| 244 | j = 0;
|
|---|
| 245 | cOutBuf[i] ^= key[j];
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | wchar_t *realOut = charEx::char_to_tchar(cOutBuf, cOutLen, realOutLen);
|
|---|
| 249 | if (static_cast<unsigned int>(realOutLen) >= *outBufLen) {
|
|---|
| 250 | LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated."));
|
|---|
| 251 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 252 | }
|
|---|
| 253 | wcsncpy(outBuf, *outBufLen, realOut);
|
|---|
| 254 | delete [] realOut;
|
|---|
| 255 | outBuf[realOutLen] = 0;
|
|---|
| 256 | *outBufLen = static_cast<unsigned int>(realOutLen);
|
|---|
| 257 | */
|
|---|
| 258 | return NSCAPI::isSuccess;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | NSCAPI::errorReturn NSAPISetSettingsString(const wchar_t* section, const wchar_t* key, const wchar_t* value) {
|
|---|
| 262 | try {
|
|---|
| 263 | settings_manager::get_settings()->set_string(section, key, value);
|
|---|
| 264 | } catch (const std::exception &e) {
|
|---|
| 265 | LOG_ERROR_STD(_T("Failed to setString: ") + key + _T(": ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 266 | return NSCAPI::hasFailed;
|
|---|
| 267 | } catch (...) {
|
|---|
| 268 | LOG_ERROR_STD(_T("Failed to setString: ") + key);
|
|---|
| 269 | return NSCAPI::hasFailed;
|
|---|
| 270 | }
|
|---|
| 271 | return NSCAPI::isSuccess;
|
|---|
| 272 | }
|
|---|
| 273 | NSCAPI::errorReturn NSAPISetSettingsInt(const wchar_t* section, const wchar_t* key, int value) {
|
|---|
| 274 | try {
|
|---|
| 275 | settings_manager::get_settings()->set_int(section, key, value);
|
|---|
| 276 | } catch (...) {
|
|---|
| 277 | LOG_ERROR_STD(_T("Failed to setInt: ") + key);
|
|---|
| 278 | return NSCAPI::hasFailed;
|
|---|
| 279 | }
|
|---|
| 280 | return NSCAPI::isSuccess;
|
|---|
| 281 | }
|
|---|
| 282 | NSCAPI::errorReturn NSAPIWriteSettings(const wchar_t* key) {
|
|---|
| 283 | try {
|
|---|
| 284 | settings::instance_ptr inst = settings_manager::get_core()->create_instance(key);
|
|---|
| 285 | if (!inst) {
|
|---|
| 286 | LOG_ERROR_STD(_T("Failed to create settings: ") + key);
|
|---|
| 287 | return NSCAPI::hasFailed;
|
|---|
| 288 | }
|
|---|
| 289 | settings_manager::get_core()->migrate_to(inst);
|
|---|
| 290 | } catch (settings::settings_exception e) {
|
|---|
| 291 | LOG_ERROR_STD(_T("Failed to write settings: ") + e.getMessage());
|
|---|
| 292 | return NSCAPI::hasFailed;
|
|---|
| 293 | } catch (...) {
|
|---|
| 294 | LOG_ERROR_STD(_T("Failed to write settings"));
|
|---|
| 295 | return NSCAPI::hasFailed;
|
|---|
| 296 | }
|
|---|
| 297 | return NSCAPI::isSuccess;
|
|---|
| 298 | }
|
|---|
| 299 | NSCAPI::errorReturn NSAPIReadSettings(const wchar_t* key) {
|
|---|
| 300 | try {
|
|---|
| 301 | settings::instance_ptr inst = settings_manager::get_core()->create_instance(key);
|
|---|
| 302 | if (!inst) {
|
|---|
| 303 | LOG_ERROR_STD(_T("Failed to create settings: ") + key);
|
|---|
| 304 | return NSCAPI::hasFailed;
|
|---|
| 305 | }
|
|---|
| 306 | settings_manager::get_core()->migrate_from(inst);
|
|---|
| 307 | settings_manager::get_settings()->reload();
|
|---|
| 308 | } catch (settings::settings_exception e) {
|
|---|
| 309 | LOG_ERROR_STD(_T("Failed to read settings: ") + e.getMessage());
|
|---|
| 310 | return NSCAPI::hasFailed;
|
|---|
| 311 | } catch (...) {
|
|---|
| 312 | LOG_ERROR_STD(_T("Failed to read settings"));
|
|---|
| 313 | return NSCAPI::hasFailed;
|
|---|
| 314 | }
|
|---|
| 315 | return NSCAPI::isSuccess;
|
|---|
| 316 | }
|
|---|
| 317 | NSCAPI::errorReturn NSAPIRehash(int flag) {
|
|---|
| 318 | return NSCAPI::hasFailed;
|
|---|
| 319 | }
|
|---|
| 320 | NSCAPI::errorReturn NSAPIDescribeCommand(const wchar_t* command, wchar_t* buffer, unsigned int bufLen) {
|
|---|
| 321 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, mainClient.describeCommand(command), NSCAPI::isSuccess);
|
|---|
| 322 | }
|
|---|
| 323 | NSCAPI::errorReturn NSAPIGetAllCommandNames(array_buffer::arrayBuffer* aBuffer, unsigned int *bufLen) {
|
|---|
| 324 | unsigned int len = 0;
|
|---|
| 325 | *aBuffer = array_buffer::list2arrayBuffer(mainClient.getAllCommandNames(), len);
|
|---|
| 326 | *bufLen = len;
|
|---|
| 327 | return NSCAPI::isSuccess;
|
|---|
| 328 | }
|
|---|
| 329 | NSCAPI::errorReturn NSAPIReleaseAllCommandNamessBuffer(wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 330 | array_buffer::destroyArrayBuffer(*aBuffer, *bufLen);
|
|---|
| 331 | *bufLen = 0;
|
|---|
| 332 | *aBuffer = NULL;
|
|---|
| 333 | return NSCAPI::isSuccess;
|
|---|
| 334 | }
|
|---|
| 335 | NSCAPI::errorReturn NSAPIRegisterCommand(unsigned int id, const wchar_t* cmd,const wchar_t* desc) {
|
|---|
| 336 | try {
|
|---|
| 337 | mainClient.registerCommand(id, cmd, desc);
|
|---|
| 338 | } catch (nsclient::commands::command_exception &e) {
|
|---|
| 339 | LOG_ERROR_STD(_T("Exception registrying command '") + cmd + _T("': ") + ::to_wstring(e.what()) + _T(", from: ") + to_wstring(id));
|
|---|
| 340 | return NSCAPI::isfalse;
|
|---|
| 341 | } catch (...) {
|
|---|
| 342 | LOG_ERROR_STD(_T("Unknown exception registrying command: ") + std::wstring(cmd) + _T(", from: ") + to_wstring(id));
|
|---|
| 343 | return NSCAPI::isfalse;
|
|---|
| 344 | }
|
|---|
| 345 | return NSCAPI::isSuccess;
|
|---|
| 346 | }
|
|---|
| 347 | NSCAPI::errorReturn NSAPISettingsRegKey(unsigned int plugin_id, const wchar_t* path, const wchar_t* key, int type, const wchar_t* title, const wchar_t* description, const wchar_t* defVal, int advanced) {
|
|---|
| 348 | try {
|
|---|
| 349 | if (type == NSCAPI::key_string)
|
|---|
| 350 | settings_manager::get_core()->register_key(plugin_id, path, key, settings::settings_core::key_string, title, description, defVal, advanced==1);
|
|---|
| 351 | if (type == NSCAPI::key_bool)
|
|---|
| 352 | settings_manager::get_core()->register_key(plugin_id, path, key, settings::settings_core::key_bool, title, description, defVal, advanced==1);
|
|---|
| 353 | if (type == NSCAPI::key_integer)
|
|---|
| 354 | settings_manager::get_core()->register_key(plugin_id, path, key, settings::settings_core::key_integer, title, description, defVal, advanced==1);
|
|---|
| 355 | return NSCAPI::hasFailed;
|
|---|
| 356 | } catch (settings::settings_exception e) {
|
|---|
| 357 | LOG_ERROR_STD(_T("Failed register key: ") + e.getMessage());
|
|---|
| 358 | return NSCAPI::hasFailed;
|
|---|
| 359 | } catch (...) {
|
|---|
| 360 | LOG_ERROR_STD(_T("Failed register key"));
|
|---|
| 361 | return NSCAPI::hasFailed;
|
|---|
| 362 | }
|
|---|
| 363 | return NSCAPI::isSuccess;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | NSCAPI::errorReturn NSAPISettingsQuery(const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) {
|
|---|
| 367 | return mainClient.settings_query(request_buffer, request_buffer_len, response_buffer, response_buffer_len);
|
|---|
| 368 | }
|
|---|
| 369 | NSCAPI::errorReturn NSAPIRegistryQuery(const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) {
|
|---|
| 370 | return mainClient.registry_query(request_buffer, request_buffer_len, response_buffer, response_buffer_len);
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 | NSCAPI::errorReturn NSAPISettingsRegPath(unsigned int plugin_id, const wchar_t* path, const wchar_t* title, const wchar_t* description, int advanced) {
|
|---|
| 375 | try {
|
|---|
| 376 | settings_manager::get_core()->register_path(plugin_id, path, title, description, advanced);
|
|---|
| 377 | } catch (settings::settings_exception e) {
|
|---|
| 378 | LOG_ERROR_STD(_T("Failed register path: ") + e.getMessage());
|
|---|
| 379 | return NSCAPI::hasFailed;
|
|---|
| 380 | } catch (...) {
|
|---|
| 381 | LOG_ERROR_STD(_T("Failed register path"));
|
|---|
| 382 | return NSCAPI::hasFailed;
|
|---|
| 383 | }
|
|---|
| 384 | return NSCAPI::isSuccess;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | //int wmain(int argc, wchar_t* argv[], wchar_t* envp[])
|
|---|
| 388 | wchar_t* copyString(const std::wstring &str) {
|
|---|
| 389 | int sz = str.size();
|
|---|
| 390 | wchar_t *tc = new wchar_t[sz+2];
|
|---|
| 391 | wcsncpy(tc, str.c_str(), sz);
|
|---|
| 392 | return tc;
|
|---|
| 393 | }
|
|---|
| 394 | NSCAPI::errorReturn NSAPIGetPluginList(int *len, NSCAPI::plugin_info *list[]) {
|
|---|
| 395 | // NSClientT::plugin_info_list plugList= mainClient.get_all_plugins();
|
|---|
| 396 | *len = 0; //plugList.size();
|
|---|
| 397 |
|
|---|
| 398 | *list = new NSCAPI::plugin_info[*len+1];
|
|---|
| 399 | /*
|
|---|
| 400 | int i=0;
|
|---|
| 401 | for(NSClientT::plugin_info_list::const_iterator cit = plugList.begin(); cit != plugList.end(); ++cit,i++) {
|
|---|
| 402 | (*list)[i].dll = copyString((*cit).dll);
|
|---|
| 403 | (*list)[i].name = copyString((*cit).name);
|
|---|
| 404 | (*list)[i].version = copyString((*cit).version);
|
|---|
| 405 | (*list)[i].description = copyString((*cit).description);
|
|---|
| 406 | }
|
|---|
| 407 | */
|
|---|
| 408 | return NSCAPI::isSuccess;
|
|---|
| 409 | }
|
|---|
| 410 | NSCAPI::errorReturn NSAPIReleasePluginList(int len, NSCAPI::plugin_info *list[]) {
|
|---|
| 411 | for (int i=0;i<len;i++) {
|
|---|
| 412 | delete [] (*list)[i].dll;
|
|---|
| 413 | delete [] (*list)[i].name;
|
|---|
| 414 | delete [] (*list)[i].version;
|
|---|
| 415 | delete [] (*list)[i].description;
|
|---|
| 416 | }
|
|---|
| 417 | delete [] *list;
|
|---|
| 418 | return NSCAPI::isSuccess;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 | NSCAPI::errorReturn NSAPIReload(const wchar_t *module) {
|
|---|
| 423 | try {
|
|---|
| 424 | return mainClient.reload(module);
|
|---|
| 425 | } catch (...) {
|
|---|
| 426 | LOG_ERROR_STD(_T("Failed to reload: ") + module);
|
|---|
| 427 | return NSCAPI::hasFailed;
|
|---|
| 428 | }
|
|---|
| 429 | return NSCAPI::isSuccess;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | NSCAPI::errorReturn NSAPISettingsSave(void) {
|
|---|
| 433 | try {
|
|---|
| 434 | settings_manager::get_settings()->save();
|
|---|
| 435 | } catch (settings::settings_exception e) {
|
|---|
| 436 | LOG_ERROR_STD(_T("Failed to save: ") + e.getMessage());
|
|---|
| 437 | return NSCAPI::hasFailed;
|
|---|
| 438 | } catch (...) {
|
|---|
| 439 | LOG_ERROR_STD(_T("Failed to save"));
|
|---|
| 440 | return NSCAPI::hasFailed;
|
|---|
| 441 | }
|
|---|
| 442 | return NSCAPI::isSuccess;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 | LPVOID NSAPILoader(const wchar_t*buffer) {
|
|---|
| 447 | if (wcscasecmp(buffer, _T("NSAPIGetApplicationName")) == 0)
|
|---|
| 448 | return reinterpret_cast<LPVOID>(&NSAPIGetApplicationName);
|
|---|
| 449 | if (wcscasecmp(buffer, _T("NSAPIGetApplicationVersionStr")) == 0)
|
|---|
| 450 | return reinterpret_cast<LPVOID>(&NSAPIGetApplicationVersionStr);
|
|---|
| 451 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsString")) == 0)
|
|---|
| 452 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsString);
|
|---|
| 453 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsSection")) == 0)
|
|---|
| 454 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsSection);
|
|---|
| 455 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsSections")) == 0)
|
|---|
| 456 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsSections);
|
|---|
| 457 | if (wcscasecmp(buffer, _T("NSAPIReleaseSettingsSectionBuffer")) == 0)
|
|---|
| 458 | return reinterpret_cast<LPVOID>(&NSAPIReleaseSettingsSectionBuffer);
|
|---|
| 459 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsInt")) == 0)
|
|---|
| 460 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsInt);
|
|---|
| 461 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsBool")) == 0)
|
|---|
| 462 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsBool);
|
|---|
| 463 | if (wcscasecmp(buffer, _T("NSAPIMessage")) == 0)
|
|---|
| 464 | return reinterpret_cast<LPVOID>(&NSAPIMessage);
|
|---|
| 465 | if (wcscasecmp(buffer, _T("NSAPISimpleMessage")) == 0)
|
|---|
| 466 | return reinterpret_cast<LPVOID>(&NSAPISimpleMessage);
|
|---|
| 467 | if (wcscasecmp(buffer, _T("NSAPIStopServer")) == 0)
|
|---|
| 468 | return reinterpret_cast<LPVOID>(&NSAPIStopServer);
|
|---|
| 469 | if (wcscasecmp(buffer, _T("NSAPIInject")) == 0)
|
|---|
| 470 | return reinterpret_cast<LPVOID>(&NSAPIInject);
|
|---|
| 471 | if (wcscasecmp(buffer, _T("NSAPIExecCommand")) == 0)
|
|---|
| 472 | return reinterpret_cast<LPVOID>(&NSAPIExecCommand);
|
|---|
| 473 | if (wcscasecmp(buffer, _T("NSAPIGetBasePath")) == 0)
|
|---|
| 474 | return reinterpret_cast<LPVOID>(&NSAPIGetBasePath);
|
|---|
| 475 | if (wcscasecmp(buffer, _T("NSAPICheckLogMessages")) == 0)
|
|---|
| 476 | return reinterpret_cast<LPVOID>(&NSAPICheckLogMessages);
|
|---|
| 477 | if (wcscasecmp(buffer, _T("NSAPIEncrypt")) == 0)
|
|---|
| 478 | return reinterpret_cast<LPVOID>(&NSAPIEncrypt);
|
|---|
| 479 | if (wcscasecmp(buffer, _T("NSAPIDecrypt")) == 0)
|
|---|
| 480 | return reinterpret_cast<LPVOID>(&NSAPIDecrypt);
|
|---|
| 481 | if (wcscasecmp(buffer, _T("NSAPISetSettingsString")) == 0)
|
|---|
| 482 | return reinterpret_cast<LPVOID>(&NSAPISetSettingsString);
|
|---|
| 483 | if (wcscasecmp(buffer, _T("NSAPISetSettingsInt")) == 0)
|
|---|
| 484 | return reinterpret_cast<LPVOID>(&NSAPISetSettingsInt);
|
|---|
| 485 | if (wcscasecmp(buffer, _T("NSAPIWriteSettings")) == 0)
|
|---|
| 486 | return reinterpret_cast<LPVOID>(&NSAPIWriteSettings);
|
|---|
| 487 | if (wcscasecmp(buffer, _T("NSAPIReadSettings")) == 0)
|
|---|
| 488 | return reinterpret_cast<LPVOID>(&NSAPIReadSettings);
|
|---|
| 489 | if (wcscasecmp(buffer, _T("NSAPIRehash")) == 0)
|
|---|
| 490 | return reinterpret_cast<LPVOID>(&NSAPIRehash);
|
|---|
| 491 | if (wcscasecmp(buffer, _T("NSAPIDescribeCommand")) == 0)
|
|---|
| 492 | return reinterpret_cast<LPVOID>(&NSAPIDescribeCommand);
|
|---|
| 493 | if (wcscasecmp(buffer, _T("NSAPIGetAllCommandNames")) == 0)
|
|---|
| 494 | return reinterpret_cast<LPVOID>(&NSAPIGetAllCommandNames);
|
|---|
| 495 | if (wcscasecmp(buffer, _T("NSAPIReleaseAllCommandNamessBuffer")) == 0)
|
|---|
| 496 | return reinterpret_cast<LPVOID>(&NSAPIReleaseAllCommandNamessBuffer);
|
|---|
| 497 | if (wcscasecmp(buffer, _T("NSAPIRegisterCommand")) == 0)
|
|---|
| 498 | return reinterpret_cast<LPVOID>(&NSAPIRegisterCommand);
|
|---|
| 499 | if (wcscasecmp(buffer, _T("NSAPISettingsRegKey")) == 0)
|
|---|
| 500 | return reinterpret_cast<LPVOID>(&NSAPISettingsRegKey);
|
|---|
| 501 | if (wcscasecmp(buffer, _T("NSAPISettingsRegPath")) == 0)
|
|---|
| 502 | return reinterpret_cast<LPVOID>(&NSAPISettingsRegPath);
|
|---|
| 503 | if (wcscasecmp(buffer, _T("NSAPIGetPluginList")) == 0)
|
|---|
| 504 | return reinterpret_cast<LPVOID>(&NSAPIGetPluginList);
|
|---|
| 505 | if (wcscasecmp(buffer, _T("NSAPIReleasePluginList")) == 0)
|
|---|
| 506 | return reinterpret_cast<LPVOID>(&NSAPIReleasePluginList);
|
|---|
| 507 | if (wcscasecmp(buffer, _T("NSAPISettingsSave")) == 0)
|
|---|
| 508 | return reinterpret_cast<LPVOID>(&NSAPISettingsSave);
|
|---|
| 509 | if (wcscasecmp(buffer, _T("NSAPINotify")) == 0)
|
|---|
| 510 | return reinterpret_cast<LPVOID>(&NSAPINotify);
|
|---|
| 511 | if (wcscasecmp(buffer, _T("NSAPIDestroyBuffer")) == 0)
|
|---|
| 512 | return reinterpret_cast<LPVOID>(&NSAPIDestroyBuffer);
|
|---|
| 513 | if (wcscasecmp(buffer, _T("NSAPIExpandPath")) == 0)
|
|---|
| 514 | return reinterpret_cast<LPVOID>(&NSAPIExpandPath);
|
|---|
| 515 | if (wcscasecmp(buffer, _T("NSAPIRegisterSubmissionListener")) == 0)
|
|---|
| 516 | return reinterpret_cast<LPVOID>(&NSAPIRegisterSubmissionListener);
|
|---|
| 517 | if (wcscasecmp(buffer, _T("NSAPIRegisterRoutingListener")) == 0)
|
|---|
| 518 | return reinterpret_cast<LPVOID>(&NSAPIRegisterRoutingListener);
|
|---|
| 519 | if (wcscasecmp(buffer, _T("NSAPIReload")) == 0)
|
|---|
| 520 | return reinterpret_cast<LPVOID>(&NSAPIReload);
|
|---|
| 521 | if (wcscasecmp(buffer, _T("NSAPIGetLoglevel")) == 0)
|
|---|
| 522 | return reinterpret_cast<LPVOID>(&NSAPIGetLoglevel);
|
|---|
| 523 | if (wcscasecmp(buffer, _T("NSAPISettingsQuery")) == 0)
|
|---|
| 524 | return reinterpret_cast<LPVOID>(&NSAPISettingsQuery);
|
|---|
| 525 | if (wcscasecmp(buffer, _T("NSAPIRegistryQuery")) == 0)
|
|---|
| 526 | return reinterpret_cast<LPVOID>(&NSAPIRegistryQuery);
|
|---|
| 527 |
|
|---|
| 528 | LOG_ERROR_STD(_T("Function not found: ") + buffer);
|
|---|
| 529 | return NULL;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | NSCAPI::errorReturn NSAPIRegisterSubmissionListener(unsigned int plugin_id, const wchar_t* channel) {
|
|---|
| 533 | return mainClient.register_submission_listener(plugin_id, channel);
|
|---|
| 534 | }
|
|---|
| 535 | NSCAPI::errorReturn NSAPIRegisterRoutingListener(unsigned int plugin_id, const wchar_t* channel) {
|
|---|
| 536 | return mainClient.register_routing_listener(plugin_id, channel);
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | NSCAPI::errorReturn NSAPINotify(const wchar_t* channel, const char* request_buffer, unsigned int request_buffer_len, char ** response_buffer, unsigned int *response_buffer_len) {
|
|---|
| 540 | std::string request (request_buffer, request_buffer_len), response;
|
|---|
| 541 | NSCAPI::nagiosReturn ret = mainClient.send_notification(channel, request, response);
|
|---|
| 542 | *response_buffer_len = response.size();
|
|---|
| 543 | if (response.empty())
|
|---|
| 544 | *response_buffer = NULL;
|
|---|
| 545 | else {
|
|---|
| 546 | *response_buffer = new char[*response_buffer_len + 10];
|
|---|
| 547 | memcpy(*response_buffer, response.c_str(), *response_buffer_len);
|
|---|
| 548 | }
|
|---|
| 549 | return ret;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | void NSAPIDestroyBuffer(char**buffer) {
|
|---|
| 553 | delete [] *buffer;
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | NSCAPI::log_level::level NSAPIGetLoglevel() {
|
|---|
| 557 | return nsclient::logging::logger::get_logger()->get_log_level();
|
|---|
| 558 | }
|
|---|